«Back to Home

Core Java

Topics

Default Methods In Java

Default Methods
 
A concept of default method implementation in interfaces introduces in Java 8. It has a capability to add for backward compatibility thus that old interfaces can be used to leverage the lambda expression capability of Java 8.
 
Such as, Collection interfaces don’t have ‘forEach’ method declaration. Thus, adding such method will just break the collection framework implementations. With the help of default method, Collection interface can contain a default implementation of ‘forEach’ method and the class implements these interfaces no need to implement the same.
 
Syntax

public interface Car {
  default void display(){
     System.out.println("Car is running fast");
   }
}
 
Multiple Defaults

With the help of default functions in interfaces, there is an option that a class is implementing two interfaces with same default methods.
 
Explanation how to ambiguity can resolve.
  1. public interface Car {  
  2.     default void display() {  
  3.         System.out.println("Car is running smooth");  
  4.     }  
  5. }  
  6. public interface Bike {  
  7.     default void display() {  
  8.         System.out.println("Bike is running fast");  
  9.     }  
  10. }  
Now, create an own method which overrides the default implementation.
  1. public class Test implements Car, Bike {  
  2.     default void display() {  
  3.         System.out.println("Car and Bike both are best");  
  4.     }  
  5. }  
Then, call the default method of the specific interface using super.
  1. public class Test implements Car, Bike {  
  2.     default void display() {  
  3.         Car.super.display();  
  4.     }  
  5. }  
Static Default Methods

An interface can have static helper methods from Java 8 beyond.
  1. public interface Car {  
  2.     default void display() {  
  3.         System.out.println("Car is running fast");  
  4.     }  
  5.     static void run() {  
  6.         System.out.println("Running……");  
  7.     }  
  8. }  
Let’s see an example of Default Method.
 
Code
  1. public class DefaultMethodsExample {  
  2.     public static void main(String args[]) {  
  3.         Car c = new Test();  
  4.         c.display();  
  5.     }  
  6. }  
  7. interface Car {  
  8.     default void display() {  
  9.         System.out.println("Car is running fast");  
  10.     }  
  11.     static void run() {  
  12.         System.out.println("Running...");  
  13.     }  
  14. }  
  15. interface Bike {  
  16.     default void display() {  
  17.         System.out.println("Bike is running smooth");  
  18.     }  
  19. }  
  20. class Test implements Car, Bike {  
  21.     public void display() {  
  22.         Car.super.display();  
  23.         Bike.super.display();  
  24.         Car.run();  
  25.         System.out.println("Hello, I am Test class");  
  26.     }  
  27. }  
18

Output

19

Summary

Thus, we learned that Default methods has a capability to add for backward compatibility thus that old interfaces can use to leverage the lambda expression capability of Java 8 and also learns how to use it in Java.