1

What is the use of default methods of an interface in Java 8?

[Social9_Share type="horizontal"]

Default Methods of an interface in java 8

In this blog let us learn about default methods introduced in interfaces in java 8 and its usage.
Let’s discuss


Let’s take a scenario. Assume we have an interface Insurance and two classes VehicleImpl and HumanImpl implementing the interface

Why default Methods are introduced in Java 8?

Now, assume we have to add a new functionality to our existing application. void policyTypes(). If we add this to our existing interface Insurance, then the contract will break. That is we need to rewrite/remodify all the implementation classes again.

One option without breaking the contract is that we can extend the existing interface and add the new functionality. In this case we need to create new classes to implement this method

Is there any other option?

Yes. The default methods introduced in java 8 comes in as a rescue, without disturbing the existing contract.

So, now let’s discuss about default methods.

What are default methods and how to use them?

Default methods are concrete methods that helps to add new functionality to the existing interfaces of our application, and ensures compatibility with the older interfaces.While adding a default method to the existing interface, it does not break the contract.

The default method comes with the keyword default. The default methods have definition and is implicitly public

default returntype methodName(){ }

So, now let us add a default method to our interface Insurance.

Once this is added to the Insurance interface, we can call this default method using the interface reference, just like calling an abstract method.

Can we override the default method in the implementing classes?

Ofcourse yes. we can override the existing default method and give a new functionality in the implementing classes.

To call the default method in the interface use interfacename.super.methodname as in Insurance.super.policyTypes().

The complete code is given below

What happens, if two interfaces have the same default method and a class implements them?

If two interfaces have the same default method, and if a class implements these two interfaces, then it will give compiler error. In this case, the implementing class should override the default method.

Can an interface having default methods extended?

Yes, the interface can be extended. Extending an interface having default methods, can be handled in three different ways.

  • Don’t add the method in the extended interface. Use it as such.
  • Redeclare the default method in the extended interface, which now becomes abstract
  • Override the default method in the extended interface, with a different functionality

Let’s discuss one by one.

Use the method in the extended interface as it is

A class that implements LifeInsurance can use/override the default method.

Redeclare the default method in the extended interface, which now becomes abstract

A class that implements LifeInsurance must implement this redeclared abstract method or the class can become abstract.The calling of these methods are as usual.

Override the default method in the extended interface, with a different functionality

A class that implements LifeInsurance can use the same overridden method or again re-implement it. However the calling of these methods are same.

So to conclude, an interface can use a default method to add a new functionality to an existing application, without modifying the existing contract and ensures compatibility with the older interfaces.

Happy Learning

Post a comment