1. Interface Concepts
The <1> interface can understand a special class, which consists of global constants and common abstract methods .
The <2> class is a concrete implementation body, and the interface defines the specification that a batch of classes need to follow, the interface does not care about the internal data of these classes, nor does it care about the implementation details of the methods in these classes, it only stipulates that certain methods must be provided in these classes .
<3> interface can be understood as a very abstract virtual base class
The <4> interface supports multiple inheritance, but rather than C + +, the interface does not produce data conflicts caused by multiple inheritance, because the interface does not allow the inclusion of normal data and allows only constants, and only specifies what methods the class needs to do without implementing it (equivalent to pure virtual functions in C + +), while C + + The virtual base class in is allowed to have its own data, which causes a data conflict problem when multiple inheritance occurs (using virtual inheritance in C + + to resolve the conflict)
the classes in <5>ps:java only support single inheritance, and multiple inheritance is implemented through interfaces
2. Interface Definition
<1> interface definitions using the interface keyword
The <2> interface defines the basic syntax:
[modifier]interface Interface Name [extends parent interface 1, parent interface 2, ...]
{
//0 to multiple constant definitions
//0 to multiple abstract method definitions
}
<3> interface is used to be inherited, implemented, modifiers are generally recommended to use public
<4> Note: You cannot use private and protected to decorate interfaces
the contents of the <5> interface are all constants or abstract methods, so there is actually an abstract keyword when defining the interface, even if no declaration of the display exists implicitly
3. Interface Definition
the properties in the <1> interface are constants, so even if the public static final modifier is not added to the definition, the system automatically adds
the methods in the <2> interface can only be abstract, so even if the public abstract modifier is not added, the system automatically adds
4. Using the interface
<1> A class can implement one or more interfaces that implement an interface using the Implements keyword. A class in Java can only inherit one parent class, which is not flexible enough to be supplemented by implementing multiple interfaces
<2> The syntax for inheriting the parent class implementation interface is:
[modifier]class class Name extends parent class implements interface 1, interface 2 ...
{
//Class body part
//If the inherited parent class is an abstract class, you need to implement the appropriate abstract method
///also need to implement an abstract method in the interface
}
<3> If you want to inherit from a parent class, the parent class must precede the interface with multiple interfaces separated by commas
<4> interfaces are often used in conjunction with anonymous internal classes, and anonymous inner classes are internal classes that do not have names, and are used to focus on implementations rather than on the name of the implementation class.
Interfaces in Java