Java Default Method

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Default Methods

Java provides a facility to create default methods inside the interface. Methods which are defined inside the interface and tagged with default are known as default methods. These methods are non-abstract methods.

Java Default Method Example

In the following example, Sayable is a functional interface that contains a default and an abstract method. The concept of default method is used to define a method with default implementation. You can override default method also to provide more specific implementation for the method.

Let's see a simple

  1. interface Sayable{  
  2.     // Default method   
  3.     default void say(){  
  4.         System.out.println("Hello, this is default method");  
  5.     }  
  6.     // Abstract method  
  7.     void sayMore(String msg);  
  8. }  
  9. public class DefaultMethods implements Sayable{  
  10.     public void sayMore(String msg){        // implementing abstract method   
  11.         System.out.println(msg);  
  12.     }  
  13.     public static void main(String[] args) {  
  14.         DefaultMethods dm = new DefaultMethods();  
  15.         dm.say();   // calling default method  
  16.         dm.sayMore("Work is worship");  // calling abstract method  
  17.   
  18.     }  
  19. }  

Output:

Hello, this is default method
Work is worship

Static Methods inside Java 8 Interface

You can also define static methods inside the interface. Static methods are used to define utility methods. The following example explain, how to implement static method in interface?

  1. interface Sayable{    
  2.     // default method    
  3.     default void say(){    
  4.         System.out.println("Hello, this is default method");    
  5.     }    
  6.     // Abstract method    
  7.     void sayMore(String msg);    
  8.     // static method    
  9.     static void sayLouder(String msg){    
  10.         System.out.println(msg);    
  11.     }    
  12. }    
  13. public class DefaultMethods implements Sayable{    
  14.     public void sayMore(String msg){     // implementing abstract method    
  15.         System.out.println(msg);    
  16.     }    
  17.     public static void main(String[] args) {    
  18.         DefaultMethods dm = new DefaultMethods();    
  19.         dm.say();                       // calling default method    
  20.         dm.sayMore("Work is worship");      // calling abstract method    
  21.         Sayable.sayLouder("Helloooo...");   // calling static method    
  22.     }    
  23. }    

Output:

Hello there
Work is worship
Helloooo...

Abstract Class vs Java 8 Interface

After having default and static methods inside the interface, we think about the need of abstract class in Java. An interface and an abstract class is almost similar except that you can create constructor in the abstract class whereas you can't do this in interface.

  1. abstract class AbstractClass{    
  2.     public AbstractClass() {        // constructor    
  3.         System.out.println("You can create constructor in abstract class");    
  4.     }    
  5.     abstract int add(int a, int b); // abstract method    
  6.     int sub(int a, int b){      // non-abstract method    
  7.         return a-b;    
  8.     }    
  9.     static int multiply(int a, int b){  // static method    
  10.         return a*b;    
  11.     }    
  12. }    
  13. public class AbstractTest extends AbstractClass{    
  14.     public int add(int a, int b){        // implementing abstract method    
  15.         return a+b;    
  16.     }    
  17.     public static void main(String[] args) {    
  18.         AbstractTest a = new AbstractTest();    
  19.         int result1 = a.add(2010);    // calling abstract method    
  20.         int result2 = a.sub(2010);    // calling non-abstract method    
  21.         int result3 = AbstractClass.multiply(2010); // calling static method    
  22.         System.out.println("Addition: "+result1);    
  23.         System.out.println("Substraction: "+result2);    
  24.         System.out.println("Multiplication: "+result3);    
  25.     }    
  26. }    

Output:

You can create constructor in abstract class
Addition: 30
Substraction: 10
Multiplication: 200