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.
Loading...
Related Quiz
- How can CSS be applied to style JavaFX components?
- If you do not define a constructor, Java provides one default constructor that initializes all instance variables with ________.
- The ________ interface of the JDBC API provides cursor support, which allows forward and backward navigation through the result set.
- What is the role of FileWriter and FileReader in character streams when dealing with file I/O?
- Which operator can be used to invert the sign of a numeric expression?