Why LinkedList
and ArrayList
extends AbstractList
in Java?
Abstract classes are used when we want to specify a common behaviour in implementation classes.
But all the methods which are in AbstractList
are overridden by ArrayList
and LinkedList
.
So what is the use of extending this class?
Class AbstractList<E> This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array).
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> where E is the type of elements maintained by this collection.
ArrayList class can be declared as follows: public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable.
ArrayList internally uses a dynamic array to store its elements. LinkedList uses Doubly Linked List to store its elements. ArrayList is slow as array manipulation is slower. LinkedList is faster being node based as not much bit shifting required.
subList(int,int)
method is not overriden by both ArrayList
and LinkedList
, and for this AbstractList
provides a common implementation
From Java source
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<E>(this, fromIndex, toIndex) :
new SubList<E>(this, fromIndex, toIndex));
}
In addition there are other methods which are not overriden like toString()
and iterator()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With