Why does List interface extend Collection interface?

collections, inheritance, interface, java

Solution

They're re-written so that they can be documented, in order to specify how the List refines the contract of these methods compared to the contract specified in the Collection interface.

For example, the `add()` method in `List` is documented to specify that the element is added to the end of the list. This can't be specified in Collection, since a Collection doesn't have a beginning and an end.

Problem

The Collection interface has multiple methods. The List interface extends the Collection interface. It declares the same methods as the Collection interface? Why is this so? For example ``` interface Collection extends Iterable { public abstract int size(); public abstract boolean isEmpty(); public abstract boolean contains(java.lang.Object); public abstract java.util.Iterator<E> iterator(); public abstract java.lang.Object[] toArray(); public abstract <T extends java/lang/Object> T[] toArray(T[]); public abstract boolean add(E); public abstract boolean remove(java.lang.Object); public abstract boolean containsAll(java.util.Collection<?>); public abstract boolean addAll(java.util.Collection<? extends E>); public abstract boolean removeAll(java.util.Collection<?>); public abstract boolean retainAll(java.util.Collection<?>); public abstract void clear(); public abstract boolean equals(java.lang.Object); public abstract int hashCode(); } ``` and same methods are also present in List interface: ``` public interface List extends Collection { public abstract int size(); public abstract boolean isEmpty(); public abstract boolean contains(java.lang.Object); public abstract java.util.Iterator<E> iterator(); public abstract java.lang.Object[] toArray(); public abstract <T extends java/lang/Object> T[] toArray(T[]); public abstract boolean add(E); public abstract boolean remove(java.lang.Object); public abstract boolean containsAll(java.util.Collection<?>); public abstract boolean addAll(java.util.Collection<? extends E>); public abstract boolean removeAll(java.util.Collection<?>); public abstract boolean retainAll(java.util.Collection<?>); public abstract void clear(); public abstract boolean equals(java.lang.Object); public abstract int hashCode(); } ``` Is it a requirement to write these methods again in List if it is already extending the Collection interface?

Original source