Explain the related knowledge of Java interface in detail
- 2021-10-11 18:18:19
- OfStack
1. Interface overview
Interface is a reference type in Java language and a collection of methods. If the class encapsulates member variables, constructors and member methods, the interface mainly encapsulates methods, including abstract methods (JDK 7 and before), default methods and static methods (JDK8) and private methods (JDK9).
2. Defining Format
Interface format
public interface Interface name {
// Abstract method
// Default method
// Static method
// Private method
}
Interface cannot be used directly, and there must be 1 "implementation class" to "implement interface".
Implementation class format:
public class Implementation class name implements Interface name {
// Override abstract method in override interface'must '
// Override default method in interface optional
}
1. The interface's implementation class must override all abstract methods that override (implement) the interface.
Implementation: Remove the abstract keyword and add curly braces to the method body.
2. Create an object that implements the class and use it.
Precautions:
If the implementation class does not override all of the abstract methods in the override interface, then the implementation class itself must be an abstract class.
2.1 Abstract Methods
Abstract method: using
abstract
Keyword modification, can be omitted, no method body. This method is used by subclass implementations.
The code is as follows:
public interface Interface name {
public abstract void Method name ();
}
Precautions:
1. Modifiers in abstract methods must be two fixed keywords: public abstract. These two keyword modifiers can be optionally omitted.
2. The three elements of the method can be defined at will.
3. The implementation class of the interface must override all abstract methods that are overridden.
2.2 Default and Static Methods
Default method: Usedefault
Keyword modification, which cannot be omitted, can be called or overridden by subclasses.
Note: The default method in the interface can solve the problem of interface upgrade.
Static method: Use thestatic
Keyword modification for the interface to call directly.
public interface Interface name {
// Default method
public default void Method name (){
// Method body
}
// Static method
public static void Method name (){
// Method body
}
}
Considerations for using static methods:
Static methods in an interface cannot be called through objects of an interface implementation class.
Correct usage: Call the static method directly through the interface name.
Format:
Interface name . Static method ( Parameter );
2.3 Private Methods
Private method: Use the
private
Decoration, which can be called by default method or static method in interface.
The format is as follows:
public interface Interface name {
// Common private method , Resolve duplicate code between multiple default methods
private Return value type Method name ( Parameter list ){
// Execute statement
}
// Static private method to solve the problem of duplicate code among multiple static methods
private static Return value type Method name ( Parameter list ){
// Execute statement
}
}
2.4 Member variables
You can also define a "member variable" in the interface, but it must be decorated with public static final 3 keywords.
From the effect point of view, this is actually a "constant" in the interface.
Format:
public static final Data type Constant name = Data value;
Precautions:
1.
public
,
static
,
final
All 3 keywords can be omitted.
2. Constants in the interface must be assigned, but cannot be assigned.
3. Constant names in the interface must use all uppercase characters separated by an underscore.
3. Interface considerations
1. Interfaces have no static code blocks or constructors.
The immediate parent class of 2.1 classes is only 1, but one class can implement multiple interfaces at the same time.
Format:
public class Class name implements Interface 1 , interface 2 , ...{
// Override overrides all abstract methods
}
3. If there are duplicate abstract methods among multiple interfaces implemented by the implementation class, you only need to override and rewrite once.
4. If the implementation class does not override all abstract methods that override all interfaces, then the implementation class must be an abstract class.
5. If there are duplicate default methods among multiple interfaces implemented by the implementation class, then implementation class 1 must override the conflicting default methods.
6.1 If the method in the direct parent class conflicts with the default method in the interface, the method in the parent class is preferred.
4. Features of the interface
Multiple implementability: One class can implement multiple interfaces Multiple inheritance: One interface can inherit multiple interfaces.Precautions:
1. If the abstract methods in multiple parent interfaces are repeated, it doesn't matter, don't worry.
2. If the default method among multiple parent interfaces is duplicated, the child interface must override the default method "with the default keyword".