Java 8
introduces “Default Method” or (Defender methods) new feature, which allows a developer to add new methods to the Interfaces
without breaking the existing implementation of these Interface
. 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 consider small example to understand how it works:
public interface OldInterface {
public void existingMethod();
default public void newDefaultMethod() {
System.out.println("New default method"
+ " is added in interface");
}
}
The following Class will compile successfully in Java JDK 8:
public class OldInterfaceImpl implements OldInterface {
public void existingMethod() {
// existing implementation is here…
}
}
If we create an instance
of OldInterfaceImpl:
OldInterfaceImpl obj = new OldInterfaceImpl ();
// print “New default method add in interface”
obj.newDefaultMethod();
Reengineering an existing JDK
framework
is always very complex. Modify one Interface
in JDK
framework
breaks all Classes
that extends
the Interface
which means that adding any new method
could break millions of lines of code
. Therefore, default methods have introduced as a mechanism to extending Interfaces
in a backward compatible
way.
Default methods can be provided to an Interface without affecting implementing Classes
as it includes an implementation. If each added method
within Interface
defined with implementation then no implementing Class
is affected. An implementing Class
can override the default implementation provided by the Interface
.
For Java 8, the JDK
collections
have been extended and forEach
method
is added to the entire collection
(which work in conjunction with lambdas). With conventional way, the code
looks like below:
public interface Iterable<T> {
public void forEach(Consumer<? super T> consumer);
}
Since this result each implementing Class with compile
errors,
as a result, a default method added with a required implementation in order that the existing implementation should not be changed.
The Iterable Interface
with the Default method is below:
public interface Iterable<T> {
public default void forEach(Consumer
<? super T> consumer) {
for (T t : this) {
consumer.accept(t);
}
}
}
The same mechanism has been used to add Stream in JDK
Interface
without breaking the implementing Classes
.
Abstract Classes versus Interfaces in Java 8
After being introduced Default Method, the Interfaces
and abstract Classes
seems similar, however, they still different concept in Java 8
. Abstract Class
can define constructor
. They are more structured and can have a state associated with them. While in contrast, default method can be implemented only in the terms of invoking other Interface methods
, with no reference to a particular implementation's state. Hence, both use for different purposes and choosing between two really depends on the scenario context.
Since java
Class
can implement multiple Interfaces
and each Interface
can define default method with same method signature
, therefore, the inherited
methods
can conflict with each other.
Consider below example:
public interface InterfaceA {
default void defaultMethod(){
System.out.println("Interface A default method");
}
}
public interface InterfaceB {
default void defaultMethod(){
System.out.println("Interface B default method");
}
}
public class Impl implements InterfaceA, InterfaceB {
}
The above code
will fail to compile
with the following error
,
java: class Impl inherits unrelated defaults for defaultMethod() from types InterfaceA and InterfaceB
In order to fix this class
, we need to provide default method implementation:
public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){
}
}
Further, if we want to invoke default implementation provided by any of super
Interface
rather than our own implementation, we can do so as follows:
public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){
// existing code here.
InterfaceA.super.defaultMethod();
}
}
We can choose any default implementation or both as part of our new method.
Default Method is different from the regular method in the sense that default method comes with default modifier. Additionally, methods
in Classes
can use and modify method
arguments
as well as the fields
of their Class
but default method, on the other hand, can only access its arguments
as Interfaces
do not have any state.
In summary, Default methods enable to add new functionality to existing Interfaces without breaking older implementation of these Interfaces.
When we extend an interface that contains a default method, we can perform the following action:
- Not override the
default method
and will inherit the default method.
- Override the
default method
similar to other methods we have overridden subclasses
.
- Redeclare default method as
abstract
, which will force subclasses
to override it.