Interfaces just got much better in Java 8
We all know that Interface contains methods and variables, but the methods declared in an interface are by default abstract (only method signature nobody).If a class is implementing an interface it must provide method implementation of the methods in the interface, otherwise, the class must be declared as abstract.
Now imagine a situation where you want to add a new method to an interface later.Modifying one interface breaks all classes that implement the interface.which means adding a single method can break millions of lines of code.
In order to address this problem, Java8 introduced "Default Method" or Defender methods, which allows the user to enter a new method in the interface without breaking existing implementation of these interfaces.It provides flexibility to allow Interface define implementation which will use as default in the situation where a concrete Class fails to provide an implementation for that method.
Let's understand this with an example
When we extend an interface that contains a default method, we can perform following:-
Some References
Now imagine a situation where you want to add a new method to an interface later.Modifying one interface breaks all classes that implement the interface.which means adding a single method can break millions of lines of code.
In order to address this problem, Java8 introduced "Default Method" or Defender methods, which allows the user to enter a new method in the interface without breaking existing implementation of these interfaces.It provides flexibility to allow Interface define implementation which will use as default in the situation where a concrete Class fails to provide an implementation for that method.
Let's understand this with an example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface OldInterface { | |
public void existingMethod(); | |
default public void DefaultMethod() { | |
System.out.println("New default method" + " is added in interface"); | |
} | |
} | |
//following class compiles successfully in JDK 8 | |
public class ClassImpl implements OldInterface { | |
@Override | |
public void existingMethod() { | |
System.out.println("normal method"); | |
} | |
public static void main(String[] args) { | |
ClassImpl obj = new ClassImpl (); | |
// print “New default method add in interface” | |
obj.DefaultMethod(); | |
} | |
} |
When we extend an interface that contains a default method, we can perform following:-
- Not override the default method and will inherit the default method
- Override the default method similar to other methods we override in the subclass.
- Redeclare default method as abstract, which force subclass to override it.
Some References
- http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
- http://cr.openjdk.java.net/~briangoetz/lambda/Defender%20Methods%20v4.pdf
Comments
Post a Comment