1

Default Method in interface – Java 8

[Social9_Share type="horizontal"]

Default Methods in interface

You would have learnt a lot about interfaces in java. To start with

  • Interfaces can have only public abstract methods.
  • Interfaces can have only public static final variables
  • A class can implement multiple interfaces
  • Interfaces help to get multiple implementations
  • An interface can extend another interface
  • An interface without methods is called as Marker Interface or Tag Interface

and so on.

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

Why default Methods?

Now I need to add a new functionality to my existing application.

void policyTypes();

If I add this to the existing interface (ie) Insurance in our case, then I need to rewrite all the implementation classes again. One other option is that I can extend the existing interface and add the new functionality.

Is there any other option?

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

So, now let’s discuss about default methods.

Default Methods

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

How to use default methods?

The default method comes with the keyword default and this default method is implicitly public, as like abstract methods in an interface


default returntype methodName()

So, now I am adding a default method to my interface Insurance.

Once this is added to the Insurance interface, I can call this default method using the interface reference, like

Overriding the default methods

Can I override this default method in my implementing classes?
OfCourse yes. I have also choices to override the existing default method and give a new functionality.

Insurance.super.policyTypes() is used to call the default method in the interface.

The complete code is given below

Extending Interfaces having default methods

Suppose, I want to extend my interface having the default method, then it can be handled in three different ways.

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

Lets 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. In the next session, lets discuss about static methods in an interface that are introduced in java8

Post a comment