Default methods in Interfaces Java-8

Dhruv Saksena
3 min readSep 4, 2021

--

With the inception of Java-8, it has added for static and default methods in it’s menu, which comes really handy now. Default methods are declared with default keyword in the signature of method declaration.

Default method makes the code much cleaner in the sense that if there are “n” implementations of an interface, then all of the implementations need to override all the methods else the contract will break. With default method, the default implementation lies in the interface itself and it’s left to the classes if they want to override it or not, thereby reducing code duplicacy.

Now, we can add more default methods to the interface, without worrying about the fact that we need to override them in all the implementations just to compile the project.

Default method in Java

Prior to java-8, interface couldn’t have any implementation. They can have only declarations, but thanks to java-8 now they can have a default implementation. You don’t need to create a separate abstract class for that purpose.

Let’s say we have an interface Computable, which has a single compute method and multiple default implementations of logging-

package com.example;public interface Computable
{
void compute(String value);
default void log(String value)
{
System.out.println("Value is :: " + value);
}
default void log2(String value)
{
System.out.println("Value in log2 :: " + value);
}
}

Now let’s make a class which implements this interface-

package com.example;
public class ComputableImpl implements Computable
{
@Override
public void compute(String value) {
System.out.println("Computing value :: " + value);
}
}

Now, lets execute these methods via an Executor class-

package com.example;
public class Executor
{
public static void main(String[] args)
{
Computable computableObj = new ComputableImpl();
computableObj.compute("Hello");
computableObj.log("Log hello");
computableObj.log2("Log hello again");
}
}

The following will be the output of this-

Computing value :: Hello
Value is :: Log hello
Value in log2 :: Log hello again

As you see, we can have multiple default implementations for methods in interfaces.

Now, what about the “Diamond Problem”, because of which Java never allowed implementation within interfaces.

Let’s say we make one for interface called Computablev2 with same default method definition-

package com.example;
public interface Computablev2
{
default void log(String value)
{
System.out.println("Value from v2 :: " + value);
}
}

Now in ComputableImpl, we implement both interfaces-

package com.example;
public class ComputableImpl implements Computable,Computablev2
{
@Override
public void compute(String value) {
System.out.println("Computing value :: " + value);
}
}

As soon as I do that compiler will crib to override the default implementation of the log method-

So, this resolves the diamond problem.

How to call default implementation from the child class?

Let’s say we have overridden the default implementation in ComputableImpl class, however I want to invoke the default implementation once and in this case we have two different default implementations-

The answer is super and the way is-

<Interface Name>.super.<methodName>

package com.example;public class ComputableImpl implements Computable, Computablev2
{
@Override
public void compute(String value)
{
System.out.println("Computing value :: " + value);
}
@Override
public void log(String value)
{
Computable.super.log(value);
}
}

Is this the end of abstract class?

I will say “No” to this. An abstract class has a greater utility where it can hold a state and it has a constructor aswell, where you can initialise the state of the object. The default method is more of a convenience for removing code duplicacy. Earlier, we were forced to make an abstract class for removing code duplicacy, but not now.

--

--

No responses yet