Default methods in Java 8

While programming in java we have learned that interfaces will only have method declaration and not the implementation. But Java 8 introduces a feature, which allows us to write default implementation for methods in the interface. This feature is called as “Default Method” or “Defender Method”. In this article, we are going to explore this feature.


Now you all are thinking about what would be the purpose of this feature? And how it will help us? Right!! So here’s the answer to your questions.

As we all know interfaces are tightly coupled with their implementation class i.e.
It is not possible to add a method to the interface without breaking all its implementer classes. When we add a method to interface all its implementer classes must declare the body of this new method.
By introducing a “Lambda” feature in Java 8, which requires changes in existing java libraries such as java.util package. For example, if we add single method in java.util.List interface then it would have broken everything, not just classes in JDK but all the custom implementation that we have written using these interfaces. And we would have to make changes in n number of custom classes, so for backward compatibility, Java 8 has cleverly added a “Default Method” feature.

The concept of Virtual Extension Methods:

These Default methods feature added a new concept of Virtual Extension Methods.
A key characteristic of an extension method is that these are a virtual method just like another interface methods, which provide a default implementation in the case where implementing class is not providing method body.

Let’s see few examples

Now we will create a class, which will use default implementation given in Greet interface.

When we run this class we will get output as
Hi – Default implementation.
Now if other Greet implementation class needs to override the default sayHi method it can do so as shown below

When we run this class we will get output as
Hi – This is custom implementation.

The case shown above is the plain vanilla case. This all looks good but now question in your mind is what if I have 2 interfaces and both are having a method with the same name then how the class will determine which interface method should be called?
Below example will answer your question. Here we will create a new interface with a similar structure to Greet interface lets say “GreetNew” interface.

Now if we create a new class, that implements Greet and GreetNew interfaces. Then when we try to call sayHi method then it will give you compilation error as shown below.

In order to resolve this conflict we have to override sayHi method where we will specify which default method of which interface should be called or whether to write all together different implementation. Specify interface specific implementation i.e. GreetNew.super.sayHi() is showed below.

Output of above program is

Hi – GreetNew implementation.

Now you may be thinking of using default method feature in interface, can replace Abstract class with interface? Answer to this question is NO. There are few things, which you should consider

– Abstract class can have any of these access specifiers for their methods and variables. Where as an Interface methods will always have public access specifier and variables in an Interface are always constants.

So that’s all for this article, Enjoy the Default method feature of Java 8.
Happy learning!

Leave a Reply

Your email address will not be published. Required fields are marked *