The definition and use of interface in java

java interface (Interface)

An interface is a special type different from a class provided in java. Only abstract methods and constants can be included in the interface. Starting from JDK1.8, default methods are also allowed in the interface. Compared with classes, interfaces are more like a form of constraint, a code of conduct.

An interface is a common specification of multiple classes.
Interface is a reference data type, and the most important content is one of them: abstract methods.
Interface use steps:
1. The interface cannot be used directly, there must be an "implementation class" to "implement the interface.
Format:
public class implementation class name implements interface name { // } 2. The implementation class of the interface must be overridden (implementation) ) All abstract methods in the interface. Implementation: remove the abstract keyword and add the method body braces. 3. Create an object of the implementation class and use it.





 

Default method in interface

Starting from Java 8 , default methods are allowed in interfaces.
Format:
public default return value type method name (parameter list) { method body. } Note: The default method in the interface can solve the problem of interface upgrade.


  • The default method of the interface can be directly called by the interface implementation class object.
  • The default method of the interface can also be overridden by the interface implementation class.
  • Call the default method, if there is no implementation class, it will look up the interface

Static methods in the interface

Starting from Java 8 , static methods are allowed in interfaces.
Format: 
public static Return value type Method name (parameter list) { Method body } Tip: Just replace abstract or defaul t with static and bring the method body. | Note: You cannot call static methods in the interface through the object of the interface implementation class. Correct usage: call the static method directly through the interface name. Format: Interface name. Static method name (parameter);






Private methods in the interface

Problem description:
We need to extract a common method to solve the problem of duplication of code between the two default methods.
But this shared method should not be used by the implementation class, it should be privatized.
Solution:
Since Java 9 , private methods are allowed to be defined in interfaces.
1. Ordinary private method , solve the problem of repeated code between multiple default methods
Format:
private return value type method name (parameter list) { method body } 2. Static private method , solve the problem of repeated code in multiple static methods Format: private static return value type method name (parameter list) { method body }







 

 Define member variables in the interface

"Member variables" can also be defined in the interface, but they must be modified with the three keywords of public static final.
From the effect point of view, this is actually the [constant] of the interface.
Format:
public static final data type constant name = data value;
Remarks:
Once the final keyword is used for modification, it cannot be changed.
Note:
1. The constants in the interface can be omitted public static final. Note: It is the same if not written.
2. The constants in the interface must be assigned; they must be assigned.

3. The name of the constant in the interface, use completely uppercase letters, separated by underscores.
 

Interface content summary

In the Java 9+ version, the content of the interface can include:
1. The member variable is actually a constant, the format:.
[Public] [static] [final] Data type constant name = data value;
Note:
constants must be assigned, and once The assignment cannot be changed. , The
constant names are completely uppercase, separated by underscores.
2. The most important thing in an interface is the abstract method, the format:
[public] [abstract] Return value type method name (parameter list); 
Note: The implementation class must override all abstract methods of the interface, unless the implementation class is an abstract class.
3. Starting from Java 8, the default method is allowed to be defined in the interface, format:
[public] default Return value type method name (parameter list) {Method body}
Note: The default method can also be overwritten.
4. Starting from Java 8 , Static methods are allowed to be defined in the interface, format:
[public] static return value type method name (parameter list) {method body}
Note: It should be called by the interface name, and the static method of the interface cannot be called by the implementation class object
5. From Java 9 Initially, private methods are allowed to be defined in the interface, format:
ordinary private method: private return value type method name (parameter list) {method body}
static private method: private static return value type method name (parameter list) {method body}
Note: Private methods can only be called by the interface itself, and cannot be used by implementation classes or others.
 

You cannot write static code blocks or construct methods in the interface. (Abstract class cannot have a constructor)

When using the interface, you need to pay attention to:


1. The interface has no static code block or construction method.
2. The direct parent of a class is unique, but a class can implement multiple interfaces at the same time.
Format:
public class MyInterfaceImpl implements MyInterfaceA, MyInterfaceB { //Override and rewrite all abstract methods 3. If there are duplicate abstract methods among multiple interfaces implemented by the implementation class, then you only need to override and rewrite--times. 4. If the implementation class does not cover all abstract methods in all interfaces, then the implementation class must be an abstract class. 5. If there are duplicate default methods among the multiple interfaces implemented by the implementation class lock, then the implementation class must override the conflicting default methods. 6. If a method in the parent class directly conflicts with the default method in the interface, the method in the parent class is preferred.





 

Specific content and related details:

1. The interface is defined using the interface keyword
Insert picture description here

2. The interface cannot be instantiated directly and needs to be implemented by a specific class
Insert picture description here

3. The interface allows to define abstract methods (the abstract keyword can be omitted), the public modifier must be used (the public modifier can also be omitted), and the implementation class must override the abstract method
Insert picture description here

4. The default keyword is allowed in the interface to define the default method. You can choose to rewrite in the implementation class, or you can choose not to rewrite
Insert picture description here

5. Constants are defined in the interface, without the static keyword and final keyword (it can be defaulted, and it is automatically completed when java is compiled)
Insert picture description here

6. The implement keyword is used when the implementation class implements the interface, and more than one can be implemented at a time. For example, there is an IUsb interface, and the concrete class UsbMouse uses the implements keyword to indicate that this interface is implemented.
Insert picture description here

7. An interface can inherit other interfaces. Compared with the inheritance of classes and the inheritance between interfaces, one interface can inherit multiple interfaces at a time.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51980491/article/details/112724897