Table of Contents
Let’s take an example:
Create an interface called Decorable
1 2 3 4 5 |
public interface Decorable { void decorateWithCurtains(); } |
Now Create a class called Room which will implement this interface.
1 2 3 4 5 6 7 |
public class Room implements Decorable{ public void decorateWithCurtains() { System.out.println("Decorate room with Curtains"); } } |
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
1 2 3 4 5 6 7 |
public interface Decorable { void decorateWithCurtains(); void decorateWithPaints(); } |
In Room class, you will get following error:
What if We use default method:
1 2 3 4 5 6 7 8 9 |
public interface Decorable { void decorateWithCurtains(); default void decorateWithPaints() { System.out.println("Decorate using paints"); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public interface Decorable { default void decorateWithPaints() { System.out.println("Decorate using paints"); } } public interface Paintable { default void decorateWithPaints() { System.out.println("Decorate using paints"); } } public class Room implements Decorable,Paintable{ } |
so it will give you compile time error as below:
you can solve this compilation error by overriding decorateWithPaints method in Room class
1 2 3 4 5 6 7 8 9 |
public class Room implements Decorable,Paintable{ public void decorateWithPaints() { System.out.println("Decorate using paints"); } } |
Difference between default methods and abstract class
Introduction of default methods to interface bridge gap between [interface](https://java2blog.com/interface-in-java-with-example/ “interface”) and [abstract class](https://java2blog.com/abstract-class-java/ “abstract class”).Now interface looks very similar to abstract classes but there are still differences. Lets list them
Parameter
|
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
|