1. What is the default method and why should there be a default method?
To put it simply, an interface can have implementation methods, and there is no need for an implementation class to implement its methods. Just add the default keyword in front of the method name.
Why do we need this feature? First of all, the previous interface is a double-edged sword. The advantage is that it is oriented towards abstraction rather than specific programming. The disadvantage is that when the interface needs to be modified, all classes that implement the interface need to be modified. The current collection framework before Java 8 does not have a foreach method. The usually conceivable solution is to add new methods and implementations to the relevant interfaces in the JDK. However, for the released version, there is no way to add new methods to the interface without affecting the existing implementation. So the default method is introduced. Their purpose is to solve the problem of incompatibility between interface modifications and existing implementations.
Simple example: an interface A, the Clazz class implements interface A.
Copy the code code as follows:
public interface A {
default void foo(){
System.out.println("Calling A.foo()");
}
}
public class Clazz implements A {
public static void main(String[] args){
Clazz clazz = new Clazz();
clazz.foo();//Call A.foo()
}
}
The code compiles even though the Clazz class does not implement the foo() method. The default implementation of the foo() method is provided in interface A.
2. Comparison between abstract classes and interfaces in Java 8
After this functional feature came out, many students reacted. Java 8 interfaces have implementation methods. What is the difference between them and abstract classes? In fact, there are still some, please see the table below for comparison. .
| Similarities | Differences |
1. They are all abstract types; 2. All can have implementation methods (interfaces did not work before); 3. You can implement all methods without implementing classes or inheritors (this was not possible before, now the default methods in the interface do not need to be implemented by the implementer) | 1. Abstract classes cannot have multiple inheritance, interfaces can (whether it is multiple type inheritance or multiple behavior inheritance); 2. Abstract classes and interfaces reflect different design concepts. In fact, abstract classes represent the "is-a" relationship, and interfaces represent the "like-a" relationship; 3. The variables defined in the interface default to the public static final type, and their initial value must be given, so they cannot be redefined in the implementation class, nor can their values be changed; the variables in the abstract class default to the friendly type, and their values can be specified in the subclass. It can be redefined in the class or reassigned. |
3. Conflict Description of Multiple Inheritance
Since the same method can be introduced from different interfaces, conflicts will naturally occur. The rules for determining conflicts by default methods are as follows:
1. A method declared in a class takes precedence over any default method (classes always win)
2. Otherwise, the most specific implementation will be selected first. For example, in the example below, B rewrites A’s hello method.
The output is: Hello World from B
If you want to call the default function of A, use the new syntax X.super.m(...). Next, modify class C, implement the A interface, and rewrite a hello method, as shown below:
Copy the code code as follows:
public class C implements A{
@Override
public void hello(){
A.super.hello();
}
public static void main(String[] args){
new C().hello();
}
}
The output is: Hello World from A
4. Summary
The default method provides us with the convenience of modifying the interface without destroying the structure of the original implementation class. Currently, the collection framework of Java 8 has made extensive use of default methods to improve it. When we finally start to use the lambdas expression of Java 8, it is provided for We have a smooth transition experience. Perhaps we will see more applications of default methods in API design in the future.