4

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

I have gone through several articles Diff b/w I vs AC and questions on SO about when to use an interface vs Abstract Class.

When I stumbled upon ArrayList Class, ArrayList class extends AbstractList Class implements List interface. AbstractList and List has common methods like add(), remove() etc.

Edit: In other words, if AbstractList implements List why have Arraylist class implement List

When is this kind of combination inheritance behavior design preferred when both have common methods?

public class ArrayList<E> extends AbstractList<E>
        implements List<E>
Community
  • 1
  • 1
Srujan Kumar Gulla
  • 5,721
  • 9
  • 48
  • 78
  • 1
    *"AbstractList and List has common methods like add(), remove() etc."* => AbstractList does implement List: `public abstract class AbstractList extends AbstractCollection implements List` – assylias Jan 04 '13 at 18:32
  • AbstactList also implements List .. – PermGenError Jan 04 '13 at 18:33
  • 2
    In other words, `implements List` in the ArrayList declaration [is redundant](http://stackoverflow.com/questions/4387419/why-does-arraylist-have-implements-list). – assylias Jan 04 '13 at 18:36

3 Answers3

4

Sometime I create an abstract BaseClass which implements an interface IBaseClass and create ChildClass which extends the BaseClass. What I mean is:

public interface IBaseClass {

}

public abstract class BaseClass implements IBaseClass {

}

public class ChildClass extends BaseClass {

}

Now Say I have an interface IChildClass and ChildClass implements it.

public class ChildClass extends BaseClass implements IChildClass {

}

And say the IBaseClass has a method someMethod. Now if you want to use the instance of ChildClass by the implementing interface IChildClass like:

IChildClass obj = new ChildClass();

Then you cannot call obj.someMethod(). Then you need to do this:

public interface IChildClass extends IBaseClass {

}

You can find in Java 1.2 documentation the the interface java.util.List doesn't extends java.util.Collection, which it does in the latest version. java.util.List was the supreme interface at that time and java.util.AbstractList implements that List also the ArrayList. AbstructList in turns extends the AbstractCollection. AbstractCollection was then supreme class. Currently AbstractCollection implements Collection. So it is clear that this design pattern has been followed to keep the Java's ever expandable pattern in mind.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
3

The implements List is redundant in this case. It is possibly to make it clear this call implements List without have to navigate the class hierarchy.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

AbstractList<E> also implements List<E>, so they are the same methods.

NPE
  • 486,780
  • 108
  • 951
  • 1,012