Why would both a parent and child class implement the same interface?

后端 未结 4 1879
梦谈多话
梦谈多话 2020-11-28 09:45

I inherited some legacy Java (1.4) code and this design decision appears regularly. I can\'t understand if there\'s any purpose or reason to it.

public inter         


        
相关标签:
4条回答
  • 2020-11-28 10:21

    I actually find that design pointless. Implemented interfaces, as you stated, are just inherited, so there's no need to copy and paste "implements SomeInterface" on the children classes. It's not clearer, smarter, or whatsoever...

    0 讨论(0)
  • 2020-11-28 10:24

    As I understand interfaces (and my experimentation has reinforced), there is no purpose to having both the parent and the child implement the same interface.

    No. Technically, it is completely redundant.

    It does however document the fact that you intend SoapFacadeImpl to be a SoapFacade and it ensures that you get a compile error, if you (or someone else) decides to remove implements SoapFacade from the base class.

    You see this pattern everywhere in the standard Java Collections API. ArrayList implements List even though its base class (AbstractList) already, does. Same holds for HashSet / AbstractSet and the Set interface.

    0 讨论(0)
  • 2020-11-28 10:27

    It is nonsense, don't do it.

    Especially in a public API like java collections. It's absolutely nonsense.

    0 讨论(0)
  • 2020-11-28 10:37

    If you use the interface also as a marker. Class.getInterfaces(); will only return directly instanced interfaces.

    0 讨论(0)
提交回复
热议问题