14. Interface

 

1. abstract classes and interfaces

No. Point of comparison Abstract class interface Remark
1 Keyword class interface  
2 concept Can not be instantiated class may contain abstract methods. The definition of standards, rules, agreements  
3 Structure Construction methods, common methods, static methods, abstract methods, member variables, static properties, constants Static constants, abstract methods  
4 And subclass relationships Subclass inherits The rules for implementing the interface implementation class  
5 relationship extends implement  
6 usage Abstract class as a parent class, an abstract method, Required to achieve the agreed class implements  
7 Feature Subclasses inherit only a single parent Implementation class can implement multiple interfaces  

Method two special interface

1. static method;

2. The default method: jdk1.8 version of the new features appear. Objective: To not mandatory to achieve a certain function.

2. The relationship between class and class

1, inheritance

Inheritance refers to a class inherits from another class. As a subclass, the other is the parent class. Subclass can use the content of the parent class, they can also expand their content. (Classes, interfaces)

 public class Animal{}
 public class Cat extends Animal{}

 

2, to achieve relations

It refers to a class implement all abstract methods implemented interface. A class can implement multiple interfaces simultaneously.

 public interface A{}
 public class AImple implements A{}

 

3, dependencies

A class dependent on another class. The degree of coupling between the two classes is not. Code levels reflect: a class as an argument of a method in another class.

 public class Pet{}
 public class Girl{
     public void play(Pet pet){
         
    }
 }

 

4, relationship

Dependencies between two classes relatively strong. Code levels reflect: a class as a property of another class.

 public class Book{}
 public class Person{
  Book book;
 }

 

Special relationship: polymerized composition. . .

 

Guess you like

Origin www.cnblogs.com/leciel/p/12669516.html