Object-oriented interfaces --Java base (23)

interface

Interface syntax:

public interface  Interface name {// interface keyword modified class

    

}

Note: This interface either-Interface

    Regular exchange between the programmer interface: communication interface (the latter will tell you)

    The key interface modification of the interface: java in the foundation, is an extension of the design

Benefits interface

1, to solve the limitations of single inheritance

2, the design and implementation of separation of foreign (the caller) hides the implementation (usually the caller does not need to be concerned to achieve)

3, the core oriented programming interface of OOP

Notes interface

Interface: Only single inheritance between classes, and interfaces to break this limit.

     1, the interface can not be instantiated

     2, the properties of the interface which is a static constant (plus default public static final ... ..), which method is an abstract method (default public abstract ......)

     3, the interface implementation class must implement all the methods, in addition to the implementation class is an abstract class.

     4, there may be a plurality of interfaces implementation class

     5, no interface constructor

The difference between abstract classes and interfaces

1, there may be an abstract class constructor, interface can not have a constructor . (Both can not be directly instantiated)

2, abstract class can have an ordinary member variables, the interface is not an ordinary member variables. It is a static constant

3, non-abstract class may contain abstract common method, can not have non-abstract general method (general method can have jdk1.8 and above, but must be added default keyword modification).

4, the access type of abstract methods abstract class may be public, protected, but the interface is a public abstract method only type, and the default is the public abstract type.

5, the abstract class may contain static methods, static methods can not contain an interface (jdk1.8 + can do)

6, abstract classes and interfaces can contain static member variables, static member variables of type of access abstract class is arbitrary, but variables defined in the interface can only be public static final type, and the default is the public static final type.

7, a class can implement multiple interfaces, but can only inherit an abstract class.

Guess you like

Origin www.cnblogs.com/Unlimited-Rain/p/12521936.html
Recommended