khatchad
khatchad

Reputation: 3176

Why does AbstractCollection implement both Iterable and Collection?

AbstractCollection implements both the Iterable and Collection interfaces. However, Collection is a subinterface of Iterable. Would it not suffice just to have AbstractCollection implement Collection?

Upvotes: 1

Views: 104

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201537

Yes. It would suffice. But, explicitly listing both allows one to tell (by simple inspection) that AbstractCollection implements both Iterable and Collection (also, because it's abstract it doesn't necessarily implement either interface - but any concrete sub-class will).

Upvotes: 0

rgettman
rgettman

Reputation: 178343

The Javadocs for AbstractCollection could be interpreted that AbstractCollection directly implements Collection and Iterable.

All Implemented Interfaces:

Iterable, Collection

However, a quick look at the source code indicates that it only directly implements Collection.

public abstract class AbstractCollection<E> implements Collection<E> {

Therefore, the Javadocs must be interpreted as saying that the class implements the given interfaces directly or indirectly. As you've indicated, there would be no need for AbstractCollection to implement Iterable directly, because it already implements Collection. The source code shows that it doesn't implement Iterable directly. It does suffice for AbstractCollection to implement only Collection directly.

Upvotes: 4

Related Questions