May 18, 2022

Default Methods in Java Interface

Java 8 onward a new feature has been added to the interface in Java known as default method in Java interface. With the inclusion of default methods, an interface in Java can have a default implementation for a method. Note that in a normal traditional form of interface all the methods are abstract with no body, now with default method you can even have method implementation with in the interface.

How is default method defined

To define an interface default method in Java you need to use default keyword with in the method signature.

Interface default method syntax
default return_type methodName(arguments) {
  ..
  ..
}

Why is default method included in interface

The inclusion of default method in interfaces makes it easy for interfaces to evolve without breaking the existing implementations. For example suppose there is a library in which there is an interface which is implemented by many classes. Now a new version of library has been released with inclusion of new method in interface for a new functionality.

Any class that uses this new version of interface is forced to implement the new method even if that new functionality is not needed. With default method in Java interface a class implementing that interface can use the default implementation itself rather than being forced to provide an implementation of its own.

Java Interface default method example

public interface MyInterface {
  int add(int a, int b);
  // interface default method
  default void showMessage(String msg) {
    System.out.println("Default method message- " + msg);
  }
}

Interface MyInterface has one abstract method and one default method. So the class implementing this interface has to implement add() method which is abstract but free to use the default implementation of the showMessage() method.

public class MainClass implements MyInterface{
  @Override
  public int add(int a, int b) {
    return a + b;
  }
	
  public static void main(String[] args) {
    MainClass obj = new MainClass();
    int result = obj.add(14, 22);
    System.out.println("result- " + result);
    // using default method in the interface
    obj.showMessage("Use the default method implementation");
  }
}
Output
result- 36
Default method message- Use the default method implementation

Implementing the interface default method

Implementing class can also provide its own implementation of the default method rather than using the default one.

public class MainClass implements MyInterface{

  @Override
  public int add(int a, int b) {
    return a + b;
  }
  /// Overriding default method
  public void showMessage(String msg) {
    System.out.println("Class implementation message- " + msg);
  }
  public static void main(String[] args) {
    MainClass obj = new MainClass();
    int result = obj.add(14, 22);
    System.out.println("result- " + result);
    
    obj.showMessage("Use the method implementation in the class");
  }
}
Output
result- 36
Class implementation message- Use the method implementation in the class

Java default methods - Resolving multiple inheritance issue

If a class implements two interfaces and both of these interfaces have a method with same name and same number of parameters and at least one of them is a default method (or both) then there is a conflict.

Let’s see an example to make it clear. There are two interfaces and both of them have a method showMessage() with the same signature. In one of the interfaces showMessage() is declared as default where as in another it is an abstract method.

public interface MyInterface {
  int add(int a, int b);
  // interface default method
  default void showMessage(String msg) {
    System.out.println("Default method message- " + msg);
  }
}

interface MySecondInterface {
  void showMessage(String msg);
}
public class MainClass implements MyInterface, MySecondInterface{
  @Override
  public int add(int a, int b) {
    return a + b;
  }

  public static void main(String[] args) {
    MainClass obj = new MainClass();
    int result = obj.add(14, 22);
    System.out.println("result- " + result);
    
    obj.showMessage("Use the method implementation in the class");
  }
}

If a class implements both of these interfaces as above then there will be a compile time error "The default method showMessage(String) inherited from MyInterface conflicts with another method inherited from MySecondInterface". This error comes as compiler can’t decide which of these two methods to use.

To resolve it there are two options-

  • Either the class implementing the interfaces should provide its own implementation of the method.
  • Call the default method in the interface using the super keyword. In the above example method is default in only one of the interface so only that method can be called using super.
Implementing class provides its own implementation
public class MainClass implements MyInterface, MySecondInterface{

  @Override
  public int add(int a, int b) {
    return a + b;
  }

  public void showMessage(String msg) {
    System.out.println("Class implementation message- " + msg);
  }
  public static void main(String[] args) {
    MainClass obj = new MainClass();
    int result = obj.add(14, 22);
    System.out.println("result- " + result);
    
    obj.showMessage("Use the method implementation in the class");
  }
}
Uses super to delegate
public class MainClass implements MyInterface, MySecondInterface{

  @Override
  public int add(int a, int b) {
    return a + b;
  }

  public void showMessage(String msg) {
    MyInterface.super.showMessage("calling interface method");
  }
  public static void main(String[] args) {
    MainClass obj = new MainClass();
    int result = obj.add(14, 22);
    System.out.println("result- " + result);
    
    obj.showMessage("Use the method implementation in the class");
  }
}

Note that in either case implementing class has to provide showMessage() method (conflicting method).

Extending interfaces having default methods

As you must be knowing an interface can extend another interface. In that case also there are certain rules if the super interface contain default methods.

  • If the sub interface doesn't mention the default method then it inherits the default method.
  • If sub interface redeclares the default method then the method becomes abstract. In that case any class implementing the sub interface must provide implementation of that method.
  • In sub interface redefine the default method, which overrides it.

For example, there is an interface MyInterface with a default method showMessage() and another interface MySecondInterface extending MyInterface. In MySecondInterface, showMessage() method is declared again thus making it an abstract method.

public interface MyInterface {
  int add(int a, int b);
  // interface default method
  default void showMessage(String msg) {
    System.out.println("Default method message- " + msg);
  }
}

interface MySecondInterface extends MyInterface {
  void showMessage(String msg);
}

In this case a class implementing MySecondInterface must provide implementation for the showMessage() method as it is abstract method in that interface.

public class MainClass implements MySecondInterface{
	
  @Override
  public int add(int a, int b) {
    return a + b;
  }

  @Override
  public void showMessage(String msg) {
    System.out.println("Message- " + msg);
  }

  public static void main(String[] args) {
    MainClass obj = new MainClass();
    int result = obj.add(14, 22);
    System.out.println("result- " + result);
    
    obj.showMessage("Use the method implementation in the class");
  }
}
Redefining the default method in the extended interface
public interface MyInterface {
  int add(int a, int b);
  // interface default method
  default void showMessage(String msg) {
    System.out.println("Default method in MyInterface message- " + msg);
  }
}

interface MySecondInterface extends MyInterface {
  default void showMessage(String msg) {
    System.out.println("Default method in MySecondInterface message- " + msg);
  }
}

That's all for the topic Default Methods in Java Interface. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment