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

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

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.


Implementing the interface default method

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


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.

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

Uses super to delegate

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.

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

Redefining the default method in the extended interface

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.