Can We Override Default Method in Java? - GeeksforGeeks

05 Feb, 2021
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 Interfaceinterface  GfG{     public default void display() {      System.out.println("GEEKSFORGEEKS");   }}  // Main Class With Implementation Of Interfacepublic 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 Oneinterface GfG{   public default void display() {      System.out.println("GEEKSFORGEEKS");   }}  // Creating Interface Twointerface gfg{     public default void display() {      System.out.println("geeksforgeeks");   }}  // Interfaces are not Overidden 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 Oneinterface GfG{   public default void display()   {      System.out.println("GEEKSFORGEEKS");   }}  // Creating Interface Twointerface gfg{     public default void display()    {      System.out.println("geeksforgeeks");   }}  public class InterfaceExample implements GfG,gfg {  // Interfaces are Overridedpublic void display() {          GfG.super.display();             gfg.super.display();   }  public static void main(String args[]) {      InterfaceExample obj = new InterfaceExample();      obj.display();   }}
GEEKSFORGEEKS
geeksforgeeks
Java 8
java-overriding
Picked
Technical Scripter 2020
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Initialize an ArrayList in Java
HashMap in Java with Examples
Interfaces in Java
Object Oriented Programming (OOPs) Concept in Java
ArrayList in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
LinkedList in Java
Stack Class in Java
Overriding in Java