Analysis of default method of new Java8 features

  • 2020-04-01 03:22:23
  • OfStack

One, what is the default method, why there is a default method

Simply put, an interface can have implementation methods, and you don't need an implementation class to implement its methods. Simply prefix the method name with the default keyword.

Why this feature? First of all, before the interface is a double-edged sword, benefit is an abstract rather than concrete programming oriented, defect is, when you need to modify the interface, you need to modify all classes implement this interface, a collection of Java 8 before the current frame without the foreach method, can think of a solution is normally in the JDK add new methods and implementation to the relevant interface. However, with a 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 interface modification incompatible with the existing implementation.

Simple example: an interface A, the Clazz class implements interface A.


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.f oo ()
    }
}


The code compiles, even if the Clazz class does not implement the foo() method. The default implementation of the foo() method is provided in interface A.

Second, Java 8 abstract class and interface comparison


After this feature came out, many students have responded that the interface of Java 8 has an implementation method, and what is the difference between abstract classes? In fact, there are some, please see the following table for comparison.

The same The difference between

1. Both are abstract types;

2. There can be an implementation method (no previous interface);

3. It is possible to implement all methods without the need of an implementation class or a successor (not before, but now the default method in the interface does not need to be implemented by the implementer).

1. Abstract classes can not be multiple inheritance, and interfaces can (whether it is multiple type inheritance or multiple behavior inheritance);

2. Different design concepts are reflected in abstract classes and interfaces. In fact, the abstract class represents the "is-a" relationship, and the interface represents the "like-a" relationship;

3. The variables defined in the interface are public static final by default, and their initial values must be given, so they cannot be redefined or changed in the implementation class; A variable in an abstract class is of friendly type by default, and its value can be redefined or reassigned in a subclass.


Iii. Conflict explanation of multiple inheritance

Since the same method can be introduced from different interfaces, conflicts will occur naturally. The default method has the following rules to judge conflicts:

1. A method declared in a class takes precedence over any default method (classes always win)

2. Otherwise, the most specific implementation is selected in preference, such as the following example: B overrides A's hello method.
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201406/201469110943741.png? 20145911101 ">

The output is: Hello World from B

If you want to call the default function of A, use the new syntax x.uper.m (...) , now modify the C class, implement the A interface, and override the hello method 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

Four,

The default methods allow us to modify the interface without breaking the structure of the original implementation class, and the current Java 8 collection framework has made extensive use of the default methods to improve it, providing a smooth transition when we finally start using the lambdas expression of Java 8. Perhaps we'll see more of the default methods used in API design in the future.


Related articles: