combination of extend abstract class and implement interface

15,820

Solution 1

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.

Solution 2

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.

Share:
15,820

Related videos on Youtube

Srujan Kumar Gulla
Author by

Srujan Kumar Gulla

Updated on October 03, 2022

Comments

  • Srujan Kumar Gulla
    Srujan Kumar Gulla over 1 year

    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>
    
    • assylias
      assylias over 11 years
      "AbstractList and List has common methods like add(), remove() etc." => AbstractList does implement List: public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
    • assylias
      assylias over 11 years
      In other words, implements List in the ArrayList declaration is redundant.