Default and Static Methods in Interface Java 8 features

Before Java version 1.8 every method present in the interface is public and abstract by default whether we are declaring or not. We can create default and static methods in Interface in Java version 1.8 onwards. This is the most important feature introduced in Java.

Example:

interface I {
    public void m1();
    public void m2();
}

Class Test1 implements I {
    public void m1() 
    {..............
    }

    public void m2() 
    {..............
    }
}

Class Test2 implements I {
    public void m1() 
    {..............
    }

    public void m2() 
    {..............
    }
}
  • In this case if 100 classes are implemented I interface and implemented its methods. If we want to add new methods in that interface then all implemented classes are going to be affected and implemented methods are mandatory for all classes whether needed or not. Then Java people recognized this problem and resolve it in Java 1.8 version with Default methods in Interface.

Example:

interface I {
   public void m1();
   public void m2();
   default void m3() {
     ............

   }

   Class Test1 implements I {
     public void m1() {
       ............
     }

     public void m2() {
       ............
     }

   }

   Class Test2 implements I {
     public void m1() {
       .............
     }

     public void m2() {
       ............
     }
   }
 }
  • Without affecting the implementation classes of the interface if we want to add new methods to the interface (i.e. default method).
  • Default method is not compulsory to implement in implementation classes. If the implementation classes can override that method. The default method is by default available to the implementation classes if need can be called or ignored.
  • Default method can override with a “public” modifier.

Example:

interface I {
  public void m1();
  public void m2();
  public static void m3() {
    ...........

  }

  Class Test1 implements I {
    public void m1() {
      ............
    }

    public void m2() {
      .............
    }

  }

  Class Test2 implements I {
    public void m1() {
      .............
    }

    public void m2() {
      .............
    }
}
  • If the class implementation or no implementation of  Interface these classes can call a static method of interface with the interface name. There is no by default available method in the implementation class.
  • If our requirement is general utility method creation then we will go for a static method in the interface. Because Object is costly in Java.

Example:

import java.util.TreeMap;

interface Demo {
  default void m1() {
    System.out.println("Hello This is a default method");
  }
  static void m2() {
    System.out.println("Hello This is a static method");
  }
}

public class Test implements Demo {

  public static void main(String[] args) {
    Demo demo = new Test();
    //calling default method of the Demo Interface
    demo.m1();
    //calling static method of the Demo Interface
    Demo.m2();

  }
}

Output: 

Hello This is a default method
Hello This is a static method
  • In this we are creating default and static methods in Interface. This feature is provided by Java 1.8 onwards.
  • This default and static are very helpful from the developer’s point of view because an already created interface needs to define some methods which will not affect the implementation classes of this interface.

Conclusion:

This topic is explained What are the static and default methods in Interface? How to create static and default methods in the Interface? What is the need for these features in Java 8?

Leave a Comment