When I was seeing the declaration of ArrayList
class ArrayList extends AbstractList
implements List, RandomAccess
ArrayList<T>
and AbstractList<T>
implement List<T>
for different purposes.
List<T>
establishes what is required for a class to be a list.ArrayList<T>
implements List<T>
as part of defining its own interface. This is essential to what ArrayList<T>
is.ArrayList<T>
extends AbstractList<T>
as part of its own implementation. This is entirely optional: one could have implemented ArrayList<T>
from scratch without inheriting AbstractList<T>
, and the class would work in the same way.AbstractList<T>
is intended as a base class for other implementations of List<T>
. Instead of establishing an interface, it follows an existing one. AbstractList<T>
's implementation of List<T>
is not required, everything would compile and run without it just the same. However, inheriting List<T>
lets Java compiler spot potential discrepancies between the interface methods and the methods of AbstractList<T>
, so it is a very good idea for AbstractList<T>
to implement List<T>
interface.This is done for documentation purposes only, to make it immediately clear to the user of the class which interfaces the class implements.
The redundant implements
clause makes no difference to the compiler.
Well, this way you must implement List<E>
methods when you create a subclass to AbstractList
, and you can also use an ArrayList
as an AbstractList
.
There are no functional benefits to declaring them again, it does not affect the behavior in any way.
I guess it's only added to make it clearer which interfaces are implemented.
Yes. It could've been omitted. But thus it is immediately visible that it is a List. Otherwise an extra click through the code / documentation would be required. I think that's the reason - clarity.
And to add what Joeri Hendrickx commented - it is for the purpose of showing that ArrayList implements List. AbstractList in the whole picture is just for convenience and to reduce code duplication between List implementations.
Reference: Why does ArrayList have "implements List"?
Totally unnecessary. I wouldn't do it at all.
It's unclear why they did that by then. But by now apparently it's a mistake, since everybody is surprised by it when they first notice this odd redundancy.