1.

What is the default method, and why is it required?

Answer»

A method in the interface that has a predefined body is known as the default method. It uses the keyword default. default methods were introduced in Java 8 to have 'Backward Compatibility in case JDK modifies any interfaces. In case a new abstract method is added to the interface, all classes implementing the interface will break and will have to implement the new method. With default methods, there will not be any impact on the interface implementing classes. default methods can be OVERRIDDEN if NEEDED in the implementation. ALSO, it does not qualify as synchronized or final.

@FunctionalInterface // Annotation is optional public interface Foo() { // Default Method - Optional can be 0 or more public default String HelloWorld() { return "Hello WORLD"; } // SINGLE Abstract Method public void bar(); }


Discussion

No Comment Found