Posted on August 22, 2015 Categories: Java
[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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
interface Insurance { void getInsuranceCover(); } class VehicleImpl implements Insurance { @Override public void getInsuranceCover() { System.out.println("Insurance Cover for Vehicles"); } } class HumanImpl implements Insurance { @Override public void getInsuranceCover() { System.out.println("Insurance Cover for people"); } } |
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
.
|
default void policyTypes(){ System.out.println("New Policies available"); } |
Once this is added to the Insurance interface, I can call this default method using the interface reference, like
|
Insurance inref = new VehicleImpl(); inref.policyTypes(); inref = new HumanImpl(); inref.policyTypes(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class VehicleImpl implements Insurance { @Override public void getInsuranceCover() { System.out.println("Insurance Cover for Vehicles"); } @Override public void policyTypes() { System.out.println("Theft cover,comprehensive cover"); Insurance.super.policyTypes(); } } |
Insurance.super.policyTypes()
is used to call the default method in the interface.
The complete code is given below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
interface Insurance { void getInsuranceCover(); default void getPolicyTypes() { System.out.println("different policies available"); } } class VehicleImpl implements Insurance { @Override public void getInsuranceCover() { System.out.println("Insurance Cover for Vehicles"); } // overriding the default method @Override public void getPolicyTypes() { Insurance.super.getPolicyTypes(); System.out.println("Theft cover,comprehensive cover"); } } class HumanImpl implements Insurance { @Override public void getInsuranceCover() { System.out.println("Insurance cover for people"); } // using the default method as such } public class DefaultDemo1 { public static void main(String[] args) { Insurance ref = new VehicleImpl(); ref.getInsuranceCover(); ref.getPolicyTypes(); ref = new HumanImpl(); ref.getInsuranceCover(); ref.getPolicyTypes(); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
interface Insurance { void getInsuranceCover(); default void getPolicyTypes() { System.out.println("different policies available"); } } interface LifeInsurance extends Insurance { void insurLife(); //default method available implicitly } class HumanImpl implements LifeInsurance { @Override public void getInsuranceCover() { System.out.println("Insurance cover for people"); } @Override public void insurLife() { System.out.println("good cover for life"); } // using the default method as such } |
A class that implements LifeInsurance
can use/override the default method.
Redeclare the default method in the extended interface, which now becomes abstract
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
interface LifeInsurance extends Insurance { void insurLife(); // redeclaring the default method makes it as abstract void policyTypes(); } class HumanImpl implements LifeInsurance { @Override public void getInsuranceCover() { System.out.println("Insurance cover for people"); } @Override public void insurLife() { System.out.println("good cover for life"); } // must override the default method @Override public void getPolicyTypes() { System.out.println("Education policy, Marriage policy"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
interface LifeInsurance extends Insurance { void insurLife(); // overriding the default method public default void policyTypes(){ System.out.println("Provides Life cover,Medical cover etc"); } } class HumanImpl implements LifeInsurance { @Override public void getInsuranceCover() { System.out.println("Insurance cover for people"); } @Override public void insurLife() { System.out.println("good cover for life"); } // overriding the default method @Override public void getPolicyTypes() { System.out.println("Education policy, Marriage policy"); } } |
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