A java interface say "TestInterface" having 2 methods method1(), method2() is implemented by 100 different classes. Now I need to introduce a new method in TestInterface without making changes to other classes which already implemented it. How do I achieve it in java?
- 2I'd look at default methods in the interface. But "refactor the entire code base" is sometimes the only answer too.markspace– markspace2015-11-19 21:34:58 +00:00Commented Nov 19, 2015 at 21:34
- By definition, if you change the interface and don't change the implementing classes, they no longer implement that interface. Is there a reason you have to change the existing interface instead of creating a new one?azurefrog– azurefrog2015-11-19 21:35:02 +00:00Commented Nov 19, 2015 at 21:35
- I don't think there is a way without changing all the implementing classes. A new interface might be the way to go.yogidilip– yogidilip2015-11-19 21:35:53 +00:00Commented Nov 19, 2015 at 21:35
- Well, if you don't change all the implementing classes, what would you expect to happen when calling the new method?Andreas– Andreas2015-11-19 21:36:55 +00:00Commented Nov 19, 2015 at 21:36
- In java 8 you can look into default implementation stackoverflow.com/questions/18286235/…tim– tim2015-11-19 21:41:04 +00:00Commented Nov 19, 2015 at 21:41
3 Answers
In my experience, the best way to do this is often to extend your Interface
public interface TestInterfaceEx extends TestInterface
Then, you can add methods to TestInterfaceEx, have the classes you want implement that, and use
if (myinstance instanceof TestInterfaceEx) { myinstanceEx = (TestInterfaceEx) myinstance; //... } in places where you want to use this new functionality
Comments
now from java 8 you can add default method in your interface, that method(default method in the interface) is present in all the classes that will implement it....
Ex :--
public class Java8Tester { public static void main(String args[]) { Vehicle vehicle = new Car(); vehicle.print(); } } interface Vehicle { default void print() { System.out.println("I am a vehicle!"); } static void blowHorn() { System.out.println("Blowing horn!!!"); } } interface FourWheeler { default void print() { System.out.println("I am a four wheeler!"); } } class Car implements Vehicle, FourWheeler { public void print() { Vehicle.super.print(); FourWheeler.super.print(); Vehicle.blowHorn(); System.out.println("I am a car!"); } } Comments
From Java8:
For example, if several classes such as A, B, C and D implement an interface XYZInterface then if we add a new method to the XYZInterface, we have to change the code in all the classes(A, B, C and D) that implement this interface. In this example we have only four classes that implement the interface which we want to change but imagine if there are hundreds of classes implementing an interface then it would be almost impossible to change the code in all those classes. This is why in java 8, we have a new concept “default methods”. These methods can be added to any existing interface and we do not need to implement these methods in the implementation classes mandatorily, thus we can add these default methods to existing interfaces without breaking the code.
We can say that concept of default method is introduced in java 8 to add the new methods in the existing interfaces in such a way so that they are backward compatible. Backward compatibility is adding new features without breaking the old code.
The method newMethod() in MyInterface is a default method, which means we need not to implement this method in the implementation class Example. This way we can add the default methods to existing interfaces without bothering about the classes that implements these interfaces.
interface MyInterface{ /* This is a default method so we need not * to implement this method in the implementation * classes */ default void newMethod(){ System.out.println("Newly added default method"); } /* Already existing public and abstract method * We must need to implement this method in * implementation classes. */ void existingMethod(String str); } public class Example implements MyInterface{ // implementing abstract method public void existingMethod(String str){ System.out.println("String is: "+str); } public static void main(String[] args) { Example obj = new Example(); //calling the default method of interface obj.newMethod(); //calling the abstract method of interface obj.existingMethod("Java 8 is easy to learn"); } }