Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 interface SoapFacade extends iConfigurable{ }  public class SoapFacadeBase implements SoapFacade{ ... }  public class SoapFacadeImpl extends SoapFacadeBase implements SoapFacade{ ... } 

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. In this scenario, everything from SoapFacade is implemented in SoapFacadeBase, but the method in iConfigurable is implemented in SoapFacadeImpl. However, that doesn't create a need to have SoapFacadeImpl implement SoapFacade.

Is there something I don't know about interfaces that would give this pattern some purpose or benefit? Are there underlying costs beyond lack of clarity that should drive refactoring it? Or should it simply be refactored for clarity/simplicity?

like image 418
Riggy Avatar asked Apr 14 '11 19:04

Riggy


People also ask

Can parent and child class implement the same interface?

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.

What happens when two classes implement the same interface?

A class can implement multiple interfaces and many classes can implement the same interface. A class can implement multiple interfaces and many classes can implement the same interface. Final method can't be overridden. Thus, an abstract function can't be final.

How can you use both a parent class and an interface?

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface. The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

What happens if the parent and the child class have a field with same identifier?

what happens if both superclass and subclass have a field with same name? Sub class field will hide the Super class field. Hidden super class field in sub class can be accessed using super keyword.


2 Answers

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.

like image 52
aioobe Avatar answered Sep 24 '22 16:09

aioobe


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

like image 40
Howard Avatar answered Sep 23 '22 16:09

Howard