Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a method in an existing interface

Tags:

java

interface

If we have an interface which has different implementations in many classes and we must add another one new method in this interface, who would be the shorter way to solve the issue of overriding that method in all implementing classes?


1 Answers

You can take a look at default methods. The idea is that you provide a default implementation in the interface. Keep in mind that this only applies to Java 8+. If you are doing this in older versions of Java, you will have no choice but to implement the method in all classes that implement the interface.

Using default methods, Oracle were able to solve the backwards-compatibility issues involved with adding the new streaming/lambda methods to the collections API.

like image 50
Vivin Paliath Avatar answered Apr 20 '26 19:04

Vivin Paliath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!