The use of java interface interface

1. The interface is defined using interface
2. In Java, interface and class are two parallel structures
3. How to define the interface and define the members in the interface
  • 3.1 JDK7 and before, only global constants and abstract methods can be defined

Global constants: public static final. But when writing, you can omit

Abstract method: public abstract

  • 3.2 JDK8, in addition to defining global constants and abstract methods, you can also define static methods and default methods

Static method : use the static keyword to modify. You can directly call a static method through the interface and execute its method body. We often use static methods in classes that are used with each other. You can find pairs of interfaces and classes like Collection/Collections or Path/Paths in the standard library.

Default method : The default method is decorated with the default keyword. We can call new methods in existing interfaces by implementing class objects while maintaining compatibility with older versions of code. For example: Java 8 API provides rich default methods for interfaces such as Collection, List, and Comparator.

Knowledge point 1: The static method defined in the interface can only be called through the interface. CompareA.method1();
Knowledge point 2: By implementing the object of the class, the default method in the interface can be called. //If the implementation class rewrites the default method in the interface, when calling, it still calls the rewritten method s.method2(); Knowledge
point 3: If the subclass (or implementation class) inherits the parent class and implementation The default method with the same name and the same parameters is declared in the interface, // then the subclass will call the method with the same name and the same parameters in the parent class by default if it does not override this method. -->Knowledge point 4 of the class priority
principle , if the implementation class implements multiple interfaces, and the default methods with the same name and the same parameters are defined in these multiple interfaces, // then if the implementation class does not override this method, an error will be reported. -- >Interface conflict. This is Need we must override this method in the implementation class
4. The constructor cannot be defined in the interface, which means that the interface cannot be instantiated
5. In Java development, interfaces are used by allowing classes to implement (implements)
  • If the implementation class covers all the abstract methods in the interface, the implementation class can be instantiated

  • If the implementation class does not cover all the abstract methods in the interface, the implementation class is still an abstract class

6. Java classes can implement multiple interfaces ---> make up for the limitations of Java's single maintenance.

格式: class AA extends BB implements CC,DD,EE

7. Interfaces and interfaces can be inherited, and multiple inheritances are possible
8. The specific use of the interface reflects polymorphism
9. The interface can actually be regarded as a specification
10. Application of interface: proxy mode, factory mode

Guess you like

Origin blog.csdn.net/weixin_44863237/article/details/128898782