In java, how can we override the default method?

 
 
 
 
In java, how can we override the default method?
 
It is not mandatory to override the default method in Java.

If we are using just one interface during a Program then at a time we are using only one default method and at that point, Overriding isn't required as shown within the below program:
 
 
 
// 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();
}
}

Output

GEEKSFORGEEKS
 

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("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 One
interface GfG{
public default void display()
{
	System.out.println("GEEKSFORGEEKS");
}
}

// Creating Interface Two
interface gfg{

public default void display()
{
	System.out.println("geeksforgeeks");
}
}

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();
}
}

 

Submit Your Programming Assignment Details