Java8 the default method

1, Why should there be a default method

Before java 8, the degree of coupling between the interface and its implementing class is too high (tightly coupled), when it is desired is a method for adding an interface, all classes must be followed to achieve the modification. The default way to solve this problem, it can add new methods to the interface, and will not undermine the achievement of the existing interface.
For example, Java8 List interface added sort method. Before Java8, then each class that implements the interface must be defined to achieve List sort method, its implementation or inherit from the parent class. Imagine if the inheritance system List interface is very complex, so how much maintenance the entire collection of the framework are!

To this end, in Java8 we introduced a new mechanism: the interface supports methods stated with implementation.

2, an example

In the previously mentioned Java8 added Interface List sort method, its source code is as follows:

public interface List extends Collection {

// ...其他成员
    
default void sort(Comparator<? super E> c) {
  ...
  ...
}

}
You can see, this new method has the sort method body, modified by the default modifier, which is the default method interface.

Obviously, the default method is not static, it is necessary to invoke the default method by an instance of class that implements the interface.

Following a custom interface to practice using the default method.

Sized interface {public
// common abstract methods, a modified default public abstract no method body
int size ();

/*
 * 默认方法,有方法体
 * 任何一个实现了Sized接口的类都会向动继承isEmpty的实现
 */
default boolean isEmpty() {
    return this.size() == 0;
}

}
In fact, with the escalation of JDK version, API continues to evolve, the default method in Java8 the API has been heavy use of, sort method above List interface is one of them.

The difference between abstract classes and
some students may have found, in Java8 added default interface method, which is not an abstract class before it? In fact, there are differences between the two.

A class can inherit an abstract class; but a class can implement multiple interfaces.
Abstract class has instance variables, and the interface can only have class variables

3, conflict resolution

We know that the Java language in a class can only inherit from a parent class, but a class can implement multiple interfaces. With the introduction of the default method in Java8, there may be a class inherits the same multiple signature method. In this case, the class will choose which function to use it?

To address this multi-inheritance, Java8 provides the following three rules:

The priority of the highest priority method of methods in the class, the class or parent class declared in default method than any declared priority.
If you can not determine first, then the higher priority sub-interface: method signatures are the same, the interface has a default priority choose the most specific method of implementation, that is, if B inherits A, then B is even more specific than A.
Finally, if you still can not judge, inherits multiple interfaces class must explicitly call the desired coverage and methods, which explicitly choose to implement a default method of use.
Let's look at a few examples together.
scene 1:

public interface A {
    default void hello() {
        System.out.println("hello from A");
    }
}
public interface B extends A {
    default void hello() {
        System.out.println("hello from B");
    }
}
public class C implements A, B {
    public static void main(String[] args) {
        new C().hello();
    }
}

Here Insert Picture Description

We control three rules above point of view, class C main () method will output what?

Rule (1)) is not satisfied.
Since B inherits A, B it is more specific than A, the Hello B should be selected () method. So, the program will print out "hello from B".
Scenario 2:
If C inherited such a D like this, what will happen?

public class D implements A {

}
public class C extends D implements A, B {
    public static void main(String[] args) {
        new C().hello();
    }
}

Here Insert Picture Description

Similarly, we look at the shining three rules:

Although inherited C D, Method A but D default uncovered.
Subsequently, the compiler to choose A and B, since B More specifically, the program will print out "hello from B".
Scenario 3:
The above D minor modifications:

public class D implements A {
    public void hello() {
        System.out.println("hello from D");
    }
}

The results and how?

Because according to the rules (1), declared in the parent class has a higher priority, so the program will print out "hello from D".

Scenario 4:
Suppose now that B is not inherited A:

public interface A {
    default void hello() {
        System.out.println("hello from A");
    }
}
public interface B {
    default void hello() {
        System.out.println("hello from B");
    }
}
public class C implements A, B {
    public static void main(String[] args) {
        new C().hello();
    }
}

Here Insert Picture Description

In this case, because the compiler does not recognize achieve A or B, more specifically, it will throw a compile error: "C inherits unrelated defaults for hello () from types A and B".

Like this scenario to resolve the conflict, may be covered in C hello () method and the selecting method shown in Method A or B of the call.

Call as follows:

public class C extends D implements A, B {
    public void hello() {
        // 显式地选择调用接口B中的方法
        // 同理,要调用接口A中的方法,可以这样:A.super.hello()
        B.super.hello();
    }

    public static void main(String[] args) {
        // 输出 hello from B
        new C().hello();
    }
}

Scenario 5:

public interface A {
    default void hello() {
        System.out.println("hello from A");
    }
}
public interface B extends A{

}
public interface C extends A{

}
public class D implements B, C {
    public void hello() {
        new D().hello();
    }
}

Here Insert Picture Description

In this case, only a stated method can be selected, so the program will output "hello from A".
Reference:
article takes you recognize the default method Java8 interface in
Java 8 default method (Default Methods)

Published 80 original articles · won praise 140 · views 640 000 +

Guess you like

Origin blog.csdn.net/linjpg/article/details/104117948