Default method in Java is a method in java which are defined inside the interface with the keyword default is known as the default method. It is a type of non-abstract method.
This method is capable of adding backward capability so that the old interface can grasp the lambda expression capability.
Java Interface Default method is also known as Defender Method or virtual extension method.
Interfaces could have only abstract methods before Java 8. The classes separately provide implementation to these methods. So, if a new method is to be added to an interface, then its implementation code has to be provided in the class implementing the same interface. For overcoming this issue, Java 8 introduced the concept of default methods that allow the interfaces to have methods with implementation without affecting the classes that implement the interface.
Can We Override Default Method in Java?
It is not mandatory to override the default method in Java.
If we are using Only one interface in a Program then at a time we are using only a single default method and at that time Overriding is not required as shown in the below program:
Java
// Creating Interface interface GfG{ public default void display() { System.out.println( "GEEKSFORGEEKS" ); } } // Main Class With Implementation Of Interface public class InterfaceExample implements GfG{ public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); // Calling Interface obj.display(); } } |
GEEKSFORGEEKS
But when more than two Interfaces are used and both act as parent class then at that time Overriding of the Default Method is required. If we are using more than one interface and in both interfaces, if both interfaces have the same name and same structure. So at that time, one must override either one both the default method otherwise it will result in an error.
Case 1: When Two Interfaces are not overridden
Java
// Java program to demonstrate the case when // two interfaces are not overridden // Creating Interface One interface GfG{ public default void display() { System.out.println( "GEEKSFORGEEKS" ); } } // Creating Interface Two interface gfg{ public default void display() { System.out.println( "neveropen" ); } } // Interfaces are not Overridden public class InterfaceExample implements GfG,gfg { public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.display(); } } |
Output:
InterfaceExample.java:18: error: types GfG and gfg are incompatible; public class InterfaceExample implements GfG,gfg { ^ class InterfaceExample inherits unrelated defaults for display() from types GfG and gfg 1 error
Case 2: When Two Interfaces are Overridden
Java
// Java program to demonstrate the case // when two interfaces are overridden // Creating Interface One interface GfG{ public default void display() { System.out.println( "GEEKSFORGEEKS" ); } } // Creating Interface Two interface gfg{ public default void display() { System.out.println( "neveropen" ); } } public class InterfaceExample implements GfG,gfg { // Interfaces are Overrided public void display() { GfG. super .display(); gfg. super .display(); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.display(); } } |
GEEKSFORGEEKS neveropen