Below is the diagram, where, if we just consider the implementations of List,
AbstractList
maintains the core behavior of list. To introduce the new implementationclass MyList
(say) one can inheritAbstractList
and override(if necessary) required methods. By extendingAbstractList
. Additionally,class MyList
is obeying the contract to behave like a list.class MyList extends AbstractList{..}
Users can use collection hierarchy, as,
AbstractList l = new ArrayList<String>(); l.add("one"); //execute ArrayList's add method
A class can also maintain composition relation with any list implementation(at runtime), by having
AbstractList l;
as member, that gets populated at runtime, with any list implementation.
So,
I would like to understand the clear reason, Why additionally interface List<E>
is introduced?
note1: Intention is to understand, how to use interface. This not a duplicate question, because both abstract class and interface are used.I did not use the word 'instead of' or 'rather'
note2: Let us not get into java-8 default methods, as above collection hierarchy was designed with whatever was provided till java-7