Pradeep Gowtham Pradeep Gowtham - 3 months ago 12
Java Question

how to minimize number of classes to modify when interface is changed

There is an interface

Accountable
which has 2 methods. There are nearly 9 classes which implement the
Accountable
interface.

public interface Accountable{
boolean isAccountable();
float getWorth();
}


We have got a new requirement as follows:
Two more methods declarations to be added to the interface. But we need to minimize the effect on the existing classes.
I was told that we can use Adaptors to resolve the issue. But I am not sure how to do it.
Could anyone please help me solve the issue?

Answer

Using java 8 you can declare default implementation just in interface:

public interface Accountable{
  boolean isAccountable();
  float getWorth();
  default int someMethod() {return 0;}
}

If you use old java the only way is to add an abstract class as a middlware, but since java don't support multiple inheritance it could be painful.