Interface default methods in java 8

In this post, I will introduce default methods in interface which are introduced in  Java 8.
Have you ever faced a situation, when you created an interface and many classes implemented that interface and now you have need to add new methods to interface. After adding new methods, your java project will be full of compilation errors because you need to add these new methods to all classes which were implementing that interface as if a class implement an  interface then you have to implement all its methods in the class.

Lets take an example:
Create an interface called Decorable

Now Create a class called Room which will implement this interface.

Now this was your current implementation and now you have a new way of decorating and you want to add it to Decorable inteface.When you add this method to Decorable interface

In Room class, you will get following error:

What if We use default method:

As you can see, we have made decorateWithPaints() default and we will not get any error in Room class as we have provided default implementation in Decorable interface.

Why default methods

The oneliner for this could be “backward compatibility”.If JDK modifies an interface, then all classes which implements this interface will break.

For adding lambda expression in Java 8, JDK needs to add  methods(such as foreach) to List or collections Interface, but if you add this method to these interface, it will break millions lines of code as class which implements the interface, need to implement all its methods.

By adding default method in interface, you can provide default implementation of it without affecting implementing classes as it includes implementation of that method and any implementing class which needs that method can override it.

What about multiple Inheritance?

Adding default implementation to the interface can give rise to ambiguity in multiple inheritance. As two interface can provide same default method and there can be ambiguity while calling.Java 8 will give you compile time exception when this kind of situation will arise.

so it will give you compile time error as below:

you can solve this compilation error by overriding decorateWithPaints method in Room class

Difference between default methods and abstract class

Introduction of default methods to interface bridge gap between interface and abstract class.Now interface looks very similar to abstract classes but there are still differences. Lets list them

Abstract class
Interface with default methods
State of objects
Abstract class can hold state of object
Interface with default methods can not hold state of objects
Access Modifier
Abstract class methods can have public ,protected,private and default modifier
Interface methods are by default public. you can not use any other access modifier with it
Abstract class can have constructor
Interface  can not have constructor
Member variables
It can have member variables
It can not have member variable

Add Comment