15

Possible Duplicate:
Why does ArrayList have “implements List”?

I am new to java I was trying to see the hierarchy of collection interface. I found that the signature of AbstractList.java is like

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

It implements List interface. But if you look at signature of child class ArrayList.java it looks like

public class ArrayList<E> extends AbstractList<E>   implements List<E>, RandomAccess, Cloneable, java.io.Serializable

If you look parent class is already implemented List interface then why child class is again implementing same interface (List).

Is there specific reason or requirement behind this

Community
  • 1
  • 1

3 Answers3

8

Is there specific reason or requirement behind this

I don't think so. Removing the second List<E> would change absolutely nothing as far as the compiler is concerned.

My two hypotheses are:

  1. It's there for historic reasons. However, I can't find any evidence to support this.
  2. It's included for documentation purposes, to make it immediately clear than an ArrayList<E> is-a List<E> (this is probably the single most important thing to know about ArrayList).
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    I am also tried to find the answer with my teachers and peers and come to conclusion that documentation and reducing an extra click is main reason behind redundant implementation of List interface. –  Dec 20 '12 at 14:27
3

I had no idea what the answer was, so I did a bit of research. Turns out, it's for clarity.

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.

Why does ArrayList have "implements List"?

Community
  • 1
  • 1
Michael
  • 3,334
  • 20
  • 27
0

Probably just for documentation purposes, to make it clear for people who want to use ArrayList.

It is indeed redundant, since the AbstractList superclass of ArrayList already implements the List interface.

from Java Ranch

Another reason may be:

// This class captures common code shared by both the ArrayList and LinkedList.
// An abstract class is a good way to provide a partially implemented class, or
// a partial implementation of an interface.  Notice that the header of this
// class claims to implement the List interface, but the file doesn't write
// all of the methods.
// 
// Java allows this file to compile because it knows that the abstract class
// must be extended to complete it.  If a subclass of AbstractList doesn't
// implement all the rest of the List interface's methods, that subclass will 
// not compile. 

I found it here.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207