Default Method in Java Interface

  • Until java 8 concreate methods were not allowed in Interface
  • From java 8 it is allowed
  • If no access specifiers are mentioned ahead of method then is considered as Abstract
  • Hence they came up with a keyword default
  • Using default keyword ahead of a concrete class in Interface, you can create non abstract methods in Interface

Why Default method ?

  • If a new method was introduced in an Interface
  • All the implementing classes were forced to provide concrete implementation of the same.
  • To overcome this default method came into picture.

Example :

interface TestInterface
{
    // abstract method
    public void square(int a);
  
    // default method
    default void show()
    {
      System.out.println("Default Method Executed");
    }
}

Difference between Abstract class Interface after java 8

Reference

Leave a Comment