Default method vs static method in an interface in Java?


An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.

Since Java8 static methods and default methods are introduced in interfaces.

Default Methods - Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.

In short, you can access the default methods of an interface using the objects of the implementing classes.

Example

interface MyInterface{
   public static int num = 100;
   public default void display() {
      System.out.println("display method of MyInterface");
   }
}
public class InterfaceExample implements MyInterface{
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.display();
   }
}

Output

display method of MyInterface

Static methods - They are declared using the static keyword and will be loaded into the memory along with the interface. You can access static methods using the interface name.

If your interface has a static method you need to call it using the name of the interface, just like static methods of a class.

Example

In the following example, we are defining a static method in an interface and accessing it from a class implementing the interface.

interface MyInterface{
   public void demo();
   public static void display() {
      System.out.println("This is a static method");
   }
}
public class InterfaceExample{
   public void demo() {
      System.out.println("This is the implementation of the demo method");
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.demo();
      MyInterface.display();
   }
}

Output

This is the implementation of the demo method
This is a static method

Difference between static and default methods −

Calling the method

  • You can call a static method using the name of an interface.
  • To call a default method you need to use the object of the implementing class.

Overriding the method

  • If you want, you can override a default method of an interface from the implementing class.

Example

interface MyInterface{
   public static int num = 100;
   public default void display() {
      System.out.println("display method of MyInterface");
   }
}
public class InterfaceExample implements MyInterface{
   public void display() {
      System.out.println("display method of class");
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.display();
   }
}

Output

display method of class
  • You cannot override the static method of the interface; you can just access them using the name of the interface. If you try to override a static method of an interface by defining a similar method in the implementing interface, it will be considered as another (static) method of the class.

Example

interface MyInterface{
   public static void display() {
      System.out.println("Static method of the interface");
   }
}
public class InterfaceExample{
   public static void display() {
      System.out.println("Static method of the class");
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      MyInterface.display();
      InterfaceExample.display();
   }
}

Output

Static method of the interface
Static method of the class
raja
Published on 01-Aug-2019 15:09:28
Advertisements