4

Is there a difference between

public class B implements C
public class A extends B implements C    

and

public class B implements C
public class A extends B

Is it just redundant implementation? Or there are advantage to do that?

Edit

Asking this question, because I saw it in java.util package e.g.

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>

public abstract class AbstractMap<K,V> implements Map<K,V>
  • 4
    It is redundant but harmless, and completely up to your discretion. – user22815 Apr 21 '16 at 21:15
  • It's all about inheritance. Interface benefits nothing in this case. – MinhTri Apr 22 '16 at 4:34
6

For most intents and purposes you don't need to implement an interface on the derived class. However there is a difference between the two if you do reflection.

Example:

public class InterfaceTest {

    static interface I {
        public void doSomething();
    }

    static class A implements I {
        public void doSomething() {
        }    
    }    

    static class B extends A {   
    }

    public static void main(String[] args) {
        System.out.println(A.class.getInterfaces().length);

        for (Class i:A.class.getInterfaces()) {
            System.out.println(i.getName());
        }

        System.out.println(B.class.getInterfaces().length);

        for (Class i:B.class.getInterfaces()) {
            System.out.println(i.getName());
        }

        B b=new B();

        System.out.println(b instanceof I);
    }
}

This outputs:

 1 
 InterfaceTest$I 
 0 
 true

So while b is an instance of I of course, B.class.getInterfaces() does not return any. You have to traverse the class hierarchy to find all interfaces that are implemented.

An example where this is relevant is using JPA AttributeConverters with Hibernate. An attribute converter has to implement the JPA AttributeConverter interface directly at this point, implementing it on a base class does not work. While this is reported as a bug in Hibernate, it still showcases that there are slight differences depending where you implement the interface and the use case.

1

They effectively do the same thing, so generally you'd only need to implement the interface on the base class.

However, if the derived class would need to implement this interface, independent of the fact the base class also implements it, then it would not hurt to also implement it in the derived class. In other words, if the base class were to no longer implement this interface, would the derived class still need to? You might want to comment why it is also implemented in the derived class.

I'm struggling to come up with even a contrived example of where this might apply, though. Sorry no realistic examples come to mind.

Related: If you are using Java and these are "Marker Interfaces", you can instead use "Marker Annotations" which are not inherited: http://www.artima.com/weblogs/viewpost.jsp?thread=98061

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.