How to ged rid of override all class when add new method in interface

yasin :

I have interface, abstract method, and more classed and using these reference type as under.

   public interface InterfaceA {
    boolean mehod1();
    boolean method2();
    boolean newMethod(); //When added new method  
    }

public abstract class AbstractA implements InterfaceA{
// other common method

}


public Class C extend AbstractA //have to override three method
public Class D extend AbstractA //have to override three method
public Class E extend AbstractA //have to override three method but only should be override old method
public Class F extend AbstractA  //have to override three method  but only should be override old method

These classes derived AbstractA class I want to only new method in interface but only implement C and D class other should be use only old method.

How should design?

Kavitha Karunakaran :

You can handle it in two ways:

  1. If you are using Java 7 or lower, extend the InterfaceA to create a new interface
    public InterfaceB extends InterfaceA{
    // Your new methods definitions here..
    }

Let Classes C and D implement InterfaceB.

  1. If you are using Java 8 or higher, as @Slaw suggested, add default method to InterfaceA. Override the default implementation to give your custom implementation in Classes C and D

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=311392&siteId=1