I was under the impression that the extends keyword is used by subclasses to inherit from their parent class so that subclasses can inherit methods and properties from that parent class. The implements keyword is used to implement methods defined in the interface.

When I read the source code of Collection interface, I was surprised to find that interface can inherit interface. (rookie himself immediately feel very ashamed, big guy not spray, ignore me on the line)

The source code is as follows:

public interface Collection<E> extends 可迭代<E> {... }Copy the code

What’s the use of knowing that interfaces can inherit from interfaces? Got online to look for a time, see big guy’s answer. For the record :(reproduced below)

First, an interface is a high level of abstraction that specifies some behavior to be implemented or only as a token, such as the Serializable interface in Java, which is more abstract than an abstract class.

And then the understanding of inheritance, inheritance is generalization. In an inheritance hierarchy consisting of interfaces, the process from abstraction to concrete is seen from the top down. Inheritance allows us to preserve and extend the behavior defined in the parent interface. The whole inheritance hierarchy, in fact, is like a tree structure, and the deeper the tree goes, the more complex the behavior, the more things you can do.

The upper layer is the abstraction of the commonality of the lower layer, and the lower layer is the evolution of the different dimensions of the upper layer. Take Java’s collection framework as an example, as shown in the following figure:

We start with an Iterable. We return an iterator that can be used to process objects that can be iterated over, either in foreach or in a while loop. So which objects can be iterated, so there is a second layer interface, Collection, Directory Stream(in THE new NIO module in JDK1.7) can be iterated.

The JDK defines a Collection as a set of objects, called elements. Some collections allow repetition, some don’t, some are unordered, and some are ordered. The original description is as follows:

A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.

Based on these descriptions, the next layer, Set, List, Queue, is an abstraction for the different types of collections in a Collection, and its internal declaration method is as follows:

We found that there are many different approaches, and this is the evolution of the different dimensions of Collection. Responsibilities continue to be refined, and the same is true for other interfaces. This is the same as when an artist first draws an outline, and then sketches out the details bit by bit.

What does it mean for interface inheritance? Think of it this way: what would join look like without interface inheritance? If not let the interface inheritance, so all the methods of the interface on an interface, then there is only one interface, this interface rules don’t feel a bit too much, should not only responsible for returns an iterator, and if a collection, and both to define an ordered set of behaviors, and to define the unordered collection behavior, is not only to define a repeating element of the set of behaviors, You define the behavior of a collection of non-repeating elements.

Given that there is only one method to define the behavior of a collection, how should this method be implemented? Don’t say a bunch of if else statements, if a new set type is added, why not add an if else statement? This is undoubtedly a bad design. In contrast, the hierarchical interface generated through interface inheritance, hierarchical analysis, clear responsibilities, Set is Set,List is List, want to achieve that structure directly implement the corresponding interface. On the other hand, interface inheritance allows you to redefine behavior that is already defined at the top level without affecting the behavior of other interfaces at the same level.

In simple systems, interface inheritance is not necessary, but in more complex systems, such as the JDK’s collections framework, interface inheritance is a good design.