Java Interface Default Implementations

An interface is often just an implicit definition as every class defines an Interface. An interface just defines the contract of the class by specifying the methods that can be invoked. Java however also has an explicit interface type which until recently adhered to this idea, but since Java 8 an interface can have a ‘default’ implementation. This idea breaks completely with what an interface is supposed to be and fades the line between an interface and an abstract class. So what is still the point of defining an interface and using default implementations?

Sometimes I feel default implementations are used to overcome the single-inheritance limitation imposed by Java. This limitation often imposes hard restrictions and can be very annoying, but using default implementations is often not a real solution due to the remaining limitations of an interface. A real interface can often not benefit from default implementations as it has no idea about the implementation and thus can not do anything. The only real use-case should be to have optional methods which do nothing by default and only if required can be implemented. It is however important that in this case the correct behaviour of the application does not depend on it, as the default implementation is doing nothing. An example of this could be a Listener interface that has multiple hooks for different events.

public interface Listener {

    public default void onStartup() {
}

    public default void onShutDown() {
}

}

If you both extend a class and implement an interface you have to be aware of the chemistry between the abstract class and the interface. But when both the interface and abstract super class have the same methods, you always have to define the method yourself, no matter whether they have an implementation or not. It is up to you to decide what you actually want. This is to avoid having unwanted and unexpected effects.

If both implement the method you have to specify which implementation you want, or offer a complete alternate implementation. If there is only a default implementation, the compiler will still want you to implement the inherited abstract method, even though you also have the default implementation. It even gets more fun when the default implementation has a different visibility from the abstract class as you always have to select the widest of the two. If neither offer an implementation is makes sense you have to define it yourself, because there just isn’t any to fall back to.

The only case where you don’t have to define anything yourself is when there is only an implementation in the abstract class. This implementation is automatically chosen for you, just like with regular inheritance.

The conclusion is that default implementations won’t fix all of your problems and they may even introduce some more work if you combine it with inheritance that has overlapping methods.

Advertisements

One thought on “Java Interface Default Implementations

  1. Default methods where only introduced to support lambdas while keeping backward compatibility which is the top priority of the Java language. A good article explaining this is https://zeroturnaround.com/rebellabs/java-8-explained-default-methods .

    My personal view on the matter is this: Only use it to add extra methods to interfaces without breaking implementations or only use it if your interface really needs to be a Functional Interface (one with a single abstract method) for usage in lambdas but you require the extra method nonetheless for any reason.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s