3

Can implementing a interface and extending an class that implements the same interface cause any problems? (other than confusion)

For example:

interface IDo
public abstract class DoIt implements IDo
public class IllDo extends DoIt implements IDo

(please ignore that it just seems wrong)

Tinus Tate
  • 2,237
  • 2
  • 12
  • 33

1 Answers1

9

When you extend a class, you inherit its entire overall interface, including any interfaces it implements or classes it derives from. So any class extending DoIt automatically implements IDo, by the very nature of the fact it's extending a class that implements it. You can put the implements clause on the declaration, but it doesn't have any further effect on the class. There can be reasons to do it, though, as addressed in the answers to this question: It documents that you specifically intend that class to implement the interface, and means that if the superclass is modified to no longer include the implements clause, the subclass either breaks (because it's missing something the interface requires) or keeps its original contract.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Re-declaring the interface probably should throw a warning in your IDE at the very least, but its not likely to cause problems (other than probably requiring `IllDo` to override every member in `IDo` directly). – Draco18s no longer trusts SE Dec 10 '15 at 15:30
  • 1
    I couldn't decide if it would no-op (seeing the superclass having those methods already) or forcing the child class to supply its own methods. – Draco18s no longer trusts SE Dec 10 '15 at 15:34
  • Cheer TJ Crowder and @Draco18s , good to know i can now safely (but with a wrong feeling) use this code. It indeed is quite meaningless under normal circumstances, but I'm in an situation that does require this solution. – Tinus Tate Dec 10 '15 at 15:35
  • 2
    @T.J.Crowder From my point of view it is not meaningless/misleading. Actually the very popular `java.util.Collection` framework follows the same convention. [Why does ArrayList have “implements List”?](http://stackoverflow.com/a/4387445/1699979) , but I know this is purely opinion based. (Doing either way will not make any difference, finally). – rajuGT Dec 11 '15 at 14:35
  • @T.J.Crowder Yes, I agree. May be the older version supports this feature or not, I'm not sure. But, in IDE while developing/maintaining the code, if we need to see the hierarchy of `ArrayList` or here `IllDo` class, we need to look at its parent class(es) that is it of any Type (meaning implements an interface). So may be if we add `implements` clause it is easy to know its Type and easy to traverse/move around the code. (Still opinion based) :P :) – rajuGT Dec 11 '15 at 15:01
  • I've finished my implementation and this setup is actually needed to discover an osgi service component which extends a base class where the base class implements the api interface. Without implementing the api interface on the service component the service component is not found when scanning the bundle for service components. In addition i would suspect that in certain cases where you use java reflection for finding certain classes that implement interfaces this also might be necessary. But indeed under normal circumstances this is meaningless. – Tinus Tate Dec 12 '15 at 12:28
  • @TinusTate: That's **very** surprising, as there's no method in reflection that gets the interfaces a specific class **declares** (unlike, say, `getDeclaredMethods`). Just the interfaces that class implements, directly or indirectly. So OSGI might be doing something naughty, but normal reflection doesn't care as far as I know. – T.J. Crowder Dec 12 '15 at 13:05
  • Although the **redundant implemented interface** has no additionall function, but it's a good design pattern for a clarity purpose, which is adopted by `java.util.Collection` framework ([Why does ArrayList have “implements List”?](https://stackoverflow.com/questions/4387419/why-does-arraylist-have-implements-list)) – Murphy Ng Feb 11 '19 at 08:28