Home > 类库下载 > java类库 > body text

New features of Java8 - default interface method

高洛峰
Release: 2016-10-15 10:37:13
Original
1619 people have browsed it

1. The background of introducing default interface methods

 Java8 can be regarded as the version that has changed the most in the iterative process of Java version updates (we should be happy to keep pace with the times to survive), but after so many years of development And iteration, the source code of Java is already a giant, and it will definitely not be easy to work on such a huge volume. So when I saw the default interface methods of Java 8 for the first time, my first feeling was that this was a hole dug by Java designers before they filled it.
From the previous explanations, we know that java8 has added many methods to the existing interface, such as the sort(Comparator super E> c) method of List. If you follow the design ideas of interfaces before Java 8, when adding a method declaration to an interface, the class that implements the interface must add a corresponding implementation for the newly added method. Considering compatibility, this is not advisable, so it is a pitfall, and new features require adding some new methods to the interface. In order to have the best of both worlds, the designers of Java8 proposed the default interface method the concept of.
In this way, the default interface method seems to be developed for API designers, which is still far away from us ordinary developers. It is a bit TuSimple to think like this. Although we do not need to design jdk, we are still in the daily development process There will still be a need to provide APIs for other business parties to call. When we update our APIs, we can use the default method to provide more advanced functions while maintaining compatibility.

2. Definition of default interface method

 The definition of default interface method is very simple, just add a default keyword before the method definition of the interface, as follows:

public interface A {    /**
     * 默认方法定义
     */
    default void method() {
        System.out.println("This is a default method!");
    }

}
Copy after login

 When we define a default method like this, all implementations Subclasses of this interface all hold this method indirectly. Or you may feel like me that interfaces and abstract classes are becoming more and more similar, indeed, but there are still the following differences between them:

1. 一个类只能继承一个类,但是可以实现多个接口

2. 抽象类可以定义变量,而接口却不能
Copy after login

In addition to solving the problems we mentioned above, abstraction also has the following benefits:

1. For some methods that are not required by every subclass, we give it a default implementation to avoid meaningless implementations in subclasses (generally we will throw new UnsupportedException()) 2. The default method is java Multiple inheritance provides a new way (although we can only inherit one class, we can implement multiple interfaces, and now interfaces can also define default methods)

3. Conflicts and their solutions

  Because one class can Implement multiple interfaces, so when a class implements multiple interfaces, and there are two or more default methods with the same method signature in these interfaces, conflicts will occur. Java8 defines the following three principles to resolve conflicts:

1 . Explicitly declared methods in a class or parent class have a higher priority than all default methods 2. If rule 1 fails, the default method with a specific implementation closest to the current class is selected. 3. If rule 2 also fails, You need to explicitly specify the interface

The following is explained through several examples:

Example 1

public interface A {    /**
     * 默认方法定义
     */
    default void method() {
        System.out.println("A's default method!");
    }
}public interface B extends A {    /**
     * 默认方法定义
     */
    default void method() {
        System.out.println("B's default method!");
    }
}public class C implements A, B {    public static void main(String[] args) {        new C().method();
    }
}// 输出:B's default method!
Copy after login

Here because interface B is closer to C than A, and the method of B is a specific default implementation, according to the rules 2. So what is actually called here is the default method of interface B

Example 2

public class D implements A {
}public class C extends D implements A, B {    public static void main(String[] args) {        new C().method();
    }
}// 输出:B's default method!
Copy after login

Example 2 adds a class D that implements interface A on the basis of the original interfaces A and B, and then class C inherits from D , and implement A and B. Although C is closer to D here, because the specific implementation of D is in A, the default method in B is still the closest default implementation. According to rule 2, what is actually called here is The default method of B.

Example 3

// A接口不变public interface B {    /**
     * 默认方法定义
     */
    default void method() {
        System.out.println("B's default method!");
    }
}public class C implements A, B {    @Override
    public void method() {        // 必须显式指定
        B.super.method();
    }    public static void main(String[] args) {        new C().method();
    }
}
Copy after login

In Example 3, interface B no longer inherits from interface A, so at this time, the default method method() called in C is the same distance from the specific implementation distance of interfaces A and B. The compiler cannot determine, so an error is reported. This Need to be specified explicitly: B.super.method().


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!