[Java8 default method]

Excerpt from "Java8 real"

Java 8 added default method is mainly to support library designers, so that they can write more easily improved interface. This approach is important because you will encounter more and more of the default method in the interface, but relatively few actually need to write the default method of programmers, but they only help to improve the program, and not for writing any particular program, we are here or not long-winded, give you an example.

List<Apple> heavyApples1 = inventory.stream().filter((Apple a) -> a.getWeight() > 150).collect(toList());

List<Apple> heavyApples2 = inventory.parallelStream().filter((Apple a) -> a.getWeight() > 150).collect(toList());

But there is a problem: Before Java 8, List does not stream or parallelStream method, it did not implement the Collection interface, because these methods had not yet thought about it! These methods can not, the code will not compile. Change for your own interface, the simplest solution is to make Java designers 8 of the stream and add a method to join the Collection interface to achieve ArrayList class.

But if to do so for users is a nightmare. There are many alternative Collections Framework interfaces are implemented using Collection API. However, a new method is added to the interface, means that all entity classes must provide an implementation. Language designer can not control all existing implementations Collections, and you now on the dilemma: How do you change a published interface without disrupting existing achieve it? 8 Java solution is to break the last link - Interface can now include implementation class does not provide a way to achieve a signature! Who will implement it? The method of missing body is provided with an interface (and therefore have the default implementation), rather than by the implementation class. This gives designers the interface provides a way to expand the interface, without breaking existing code. Java 8 to use the new default keyword in an interface declaration to indicate this.

For example, in Java 8, you can now directly call the sort method List. It is the default method List interface shown below Java 8 implemented, it will call Collections.sort static method:

default void sort(Comparator<? super E> c) {
	Collections.sort(this, c);
}

This means any entity classes List do not need to explicitly implement sort, but in previous versions of Java, unless a sort of realization, otherwise it will fail in these entity classes recompile. But Wait, a class can implement multiple interfaces, is not it? So, if there are multiple default implementation, it means that Java has been in some form of several interfaces in multiple inheritance? Yes, to some extent it is. Java 8 with some restrictions to avoid similar to C ++ in the notorious diamond appear succession.

Published 107 original articles · won praise 88 · views 260 000 +

Guess you like

Origin blog.csdn.net/Code_shadow/article/details/103099317
Recommended