default methods in Java8

Vivek Singh
3 min readSep 12, 2021

--

What is default ?

Before Java8, interfaces had just abstract methods.
From Java8, we can write the body of the method in the interfaces by making the methods use a default keyword in the signature of the method. In simple terms, when we implement an interface which has default methods, we do not need to write the implementation details of the default method.

Example :

Interface Stream<T> {
IntStream mapToInt(ToIntFunction<? super T> mapper);
default Builder<T> add(T t) {
accept(t);
return this;
}
}

In the above example we have the body of the method in an interface using default.

Why do we need a default method ?

There are new features coming in with every release of Java. Streams is one such feature which makes life easier in Java8, as it provides a core feature of the functional programming paradigm.

We need default methods because applications which are already developed in versions < Java7 can easily migrate to Java8 or later.

We know that before Java8 we implemented the interfaces and provided the implementation details of abstract methods . So if JDK developers added abstract methods in Java8 or later for new features, then the applications developed in Java7 would break if there was a migration to Java8, because we then we would be required to implement new abstract methods. However, with default methods, as we have the body in the interface itself, it makes java applications compatible with different versions.

Two interfaces with same default method :

public interface DefaultInterface1 {
default int add(int x, int y) {
return x+y;
}
}
public interface DefaultInterface2 {
default int add(int x, int y) {
return x+y;
}
}
public class Example1 implements DefaultInterface1, DefaultInterface2{
}

Gives a compiler exception, as the compiler is unable to process which interface to use as they both have same method definitions.

To solve this, override the method defined in the interfaces inside the class :

public class Example1 implements DefaultInterface1, DefaultInterface2{
@Override
public int add(int x, int y) {
return DefaultInterface1.super.add(x, y);
//return DefaultInterface2.super.add(x, y);
}
}

Based on your use case, call the Interface you want to in the above code.

A class and a interface with same method definition

In this case, preference is given to a class.

public interface DefaultInterface1 {
default int add(int x, int y) {
return 3;
}
}
public class Example2 {
public int add(int x, int y) {
return 2;
}
}
public class Example1 extends Example2 implements DefaultInterface1 {
public static void main(String args[]) {
Example1 example1 = new Example1();
System.out.println(example1.add(2,3));
}
}

The above code outputs integer 2 as preference is given to a class.

Can have multiple defaults :

public interface DefaultInterface1 {
default int add(int x, int y) {
return x + y;
}
default int subtract(int x, int y) {
return x - y;
}
}

An interface can have multiple default methods.

Cannot use default keyword with equals method as it is already there in Object class

// compiler exception
public interface DefaultInterface1 {
default void equals(Object o) {
}
}

Throws a compiler exception as equals is already defined in anObject class, meaning we cannot create a default method which is already defined by parent class.

Conclusion :

We got a fair idea of default keyword in java8 with this tech blog. Feel free to reach out to me at vivek.sinless@gmail.com

--

--

Vivek Singh
Vivek Singh

Written by Vivek Singh

Software Developer. I write about Full Stack, NLP and Blockchain. Buy me a coffee - buymeacoffee.com/viveksinless

No responses yet