Java interface (Interface) is a series of methods declarations and is a collection of method features. An interface only has the characteristics of methods but no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions).
1. Interface meaning:
1. Java interface, the structures that exist in the Java language have specific syntax and structure;
2. A collection of features of methods that a class has is a logical abstraction.
The former is called "Java interface" and the latter is called "interface".
The Java interface itself does not have any implementation, because the Java interface does not involve appearances, but only describes public behavior, so the Java interface is more abstract than the Java abstract class.
The methods of Java interface can only be abstract and public. The Java interface cannot have a constructor. The Java interface can have public, static and final properties.
2. Why use interface Java is a single inheritance language? If you want to add new functions to specific classes with existing parent classes, under the OCP principle, the solution is to add parent classes to its parent class, or add parent classes to its parent class until it moves to the top of the class hierarchy structure. In this way, the design of insertability of a specific class becomes a modification of all classes in the entire hierarchy.
When there is an interface, in the above example, there is no need to maintain all classes in the entire hierarchy structure.
3. The interface has insertability:
Any class in a hierarchy can implement an interface, which will affect all subclasses of this class, but will not affect any superclasses of this class. This class will have to implement the methods specified by this interface, and its subclasses can automatically inherit these methods from this class, and of course they can also choose to replace all these methods, or some of them. At this time, these subclasses are pluggable (and can be loaded with this interface type, passing and implementing all its subclasses).
The interface provides interpolation and method calls. The larger the scale of the software system, the longer the life cycle. The interface ensures the flexibility and scalability of the software system and the interpolationability.
It is precisely because of the interface that Java single inheritance has the possibility of new extension (replacement of multiple inheritance); 3. Type hierarchy Java interface (and abstract classes) is generally used as the starting point of a type hierarchy.
If a class already has a major supertype, then by implementing an interface, the class can have another minor supertype, which is called a hybrid type.
4. Java interface classification
1. Ordinary interface (including method definition) public interface ActionListener{public abstract void actionPerformed(ActionEvent event);}
2. Identification interface (no method and attribute definition) Identification interface is an interface without any methods and attributes. Identification interface does not have any semantic requirements for the class that implements it. It only indicates that the class that implements it belongs to a specific type.
public interface Serializable{};3. Constant interface refers to using a Java interface to declare some constants, and then the classes that implement this interface use these constants.
public interface AppConstants{public static final DATA_SOURCE_NAME="test";public static final USER_NAME="test";public static final PASSWORD="test";}
5. Characteristics of the interface
1. The member variables in the Java interface are of public, static, and final by default (all of which can be omitted), and must be displayed and initialized, that is, the member variables in the interface are constants (caps, separated by "_" between words)
2. The methods in the Java interface are of public and abstract type by default (all can be omitted). Without method bodies, they cannot be instantiated.
3. The Java interface can only contain member variables of public, static, final types and member methods of public, abstract types.
4. There is no constructor in the interface and cannot be instantiated.
5. One interface cannot implement another interface, but it can inherit multiple other interfaces.
6. The Java interface must implement its abstract methods through classes.
7. When a class implements a certain Java interface, it must implement all abstract methods in the interface, otherwise this class must be declared as an abstract class
8. It is not allowed to create an instance of the interface (instance) but it is allowed to define a reference variable of the interface type. This reference variable refers to an instance of the class that implements this interface.
9. A class can only inherit one direct parent class, but can implement multiple interfaces, indirectly implement multiple inheritance.
6. The difference between Java interface and Java abstract classes The focus of object-oriented design is abstraction. Both abstract classes and interfaces are located on the upper layer of the inheritance tree.
Similarities:
1. Represents the abstraction layer of the system. When a system uses a class on an inheritance tree, it should try to declare the reference variable as the upper abstract type of the inheritance tree. This can improve the coupling between the two systems. 2. Neither can be instantiated. 3. Both contain abstract methods. These abstract methods are used to describe what services the system can provide, but do not contain the differences in the method body:
1. The biggest difference is that the Java abstract class can provide partial implementations of certain methods, while the Java interface cannot; this is probably the only advantage of the Java abstract class, but this advantage is very useful.
You can add a new concrete method to the abstract class, and all subclasses will automatically obtain this method; but add a new method to the Java interface, and all classes that implement this interface cannot be successfully compiled, and you must manually add the implementation of this method to each class that implements the interface;
2. The implementation of an abstract class can only be given by a subclass, that is, the implementation can only be in the inherited hierarchical structure defined by the abstract class; therefore, the effectiveness of an abstract class as a type definition tool is greatly reduced.
Java interface. Any class that implements the methods specified by a Java interface can have the type of this interface, and a class can implement any multiple Java interfaces, so that this class has multiple types.
As can be seen above, the Java interface is an ideal tool for defining mixed types. Mixed classes indicate that a class has not only the behavior of a certain primary type, but also other secondary behaviors.
3. Combining the respective advantages of abstract classes and Java interfaces in points 1 and 2, a classic design pattern will come out:
The work of declaring a type is still undertaken by the Java interface, but at the same time a Java abstract class is given and this interface is implemented. Other concrete classes that also belong to this abstract type can choose to implement this Java interface or inherit this abstract class. That is to say, in the hierarchy, the Java interface is at the top and then followed by the abstract class. The biggest advantages of these two can be maximized. This mode is the "default adaptation mode".
This pattern is used in the Java language API, and it all follows certain naming specifications: Abstract + interface name.
7. The overall principle of using interfaces and abstract classes:
1. Use the interface as the window for interaction between the system and the outside world. Standing from the perspective of the outside user (another system), the interface promises the user what services the system can provide, and from the perspective of the system itself, which services the interface must implement. The interface is the highest level abstract type in the system. Through interface interaction, the coupling system A between the two systems can be improved to interact through system B. This means that when system A accesses system B, the reference variable declares the reference variable as the interface type in system B. This reference variable refers to the instance of the implementation class of the interface in system B.
public interface B { }
public class C implements B { }
public class A { B a = new C(); }
2. The Java interface itself must be very stable. Once the Java interface is formulated, it is not allowed to go as it happens, otherwise it will have an impact on external users and the system itself. 3. Use abstract classes to customize the extension points in the system, and abstract classes to complete partial implementation. Some functions must be implemented through its subclasses.
The above article details the concepts, classifications and differences from abstract categories of Java interfaces are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.