8

I have a tricky question on interface. Please try to give me a solution for it.

Scenario: I have written an interface with five methods. Also I have implemented more than 100 classes using this interface. Now, I need to add one more method to the interface. Consequently, I will need to define the same method in all classes. How can I avoid this???

Please reply...

Thanks, Akif

Akif
  • 81
  • 2

2 Answers2

25

Could you avoid adding a method to the interface by instead creating a new interface which inherits from that first interface and then only changing the classes you need that new method on? Hence, if foo didn't need the new method, leave it alone but if bar did, change it to the new interface.

wheaties
  • 35,646
  • 15
  • 94
  • 131
0

Java 8 has default method which you could add to an interface

https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

All the subclasses which do not override this method will resort to the default implementation in the interface

Ganesh Krishnan
  • 7,155
  • 2
  • 44
  • 52