Adding a Method to an Existing Interface
You can look at the default methods . The idea is that you provide a default implementation in the interface. Be aware that this only applies to Java 8+. If you do this in older versions of Java, you will have no choice but to implement this method in all classes that implement the interface.
By using the default methods, Oracle was able to address backward compatibility issues associated with adding new streaming / lambda methods to the collection API.
source to share
When you add a new method to an interface, you need to implement that method in all classes that implement your interface.
In Java 8, you can create a method default
with an implementation. For example:
interface YourInterface {
default void method() {
// do something
}
}
class YourClass implements YourInterface {
public static void main(String[] args) {
YourClass yourClass = new YourClass();
yourClass.method();
}
}
You can read about default methods in the Oracle Tutorials .
source to share