Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why LinkedList and arraylist extends AbstractList in java?

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?

like image 299
Raj Avatar asked Aug 23 '13 05:08

Raj


People also ask

What is the use of AbstractList in Java?

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).

Which class extends the AbstractList class and implements the interfaces?

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> where E is the type of elements maintained by this collection.

How can you declare that a class ArrayList can use the methods of the AbstractList class is of type serializable and provides implementations for the methods of list?

ArrayList class can be declared as follows: public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable.

Why ArrayList is faster than LinkedList?

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.


1 Answers

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()

like image 110
sanbhat Avatar answered Oct 14 '22 15:10

sanbhat