In a scenario where you need to add a new method to an interface that is implemented by dozens of classes without breaking existing functionality, how would you achieve this in Java 8 and above?

  • Add a default method to the interface, providing a default implementation for the new method. This way, existing implementations won't be affected, and classes can choose to override the default method if needed.
  • Create a separate utility class with the new method and have implementing classes use this utility class to access the new functionality.
  • It's not possible to add a new method to an existing interface without breaking existing functionality in Java 8 and above.
  • Use a combination of a new interface that extends the existing one with the new method and update all implementing classes to implement the new interface.
In Java 8 and above, you can add a new method to an existing interface by providing a default implementation for the new method. Existing classes that implement the interface won't be affected and can choose to override the new method if needed. This ensures backward compatibility without breaking existing functionality.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *