Default and Static Methods in Interfaces
This overview of both default and static methods covers why default methods are necessary with interfaces and how they interact with inheritance and multiple inheritance.
Join the DZone community and get the full member experience.
Join For FreeJava 8 introduced default and static methods in interfaces. This feature enables us to add new functionality in the interfaces without breaking the existing contract of the implementing classes.
How to Define Default and Static Methods?
Default methods have default
, and static methods have thestatic
keyword in the method signature.
public interface Parser {
List<String> parse(File file);
// default implementation valid for all the file parser viz. JsonFileParser, CsvFileParser, TextFileParser
default boolean canParse(File file) {
return Objects.nonNull(file) && file.getName().endsWith(getFileType().getExtension());
}
FileType getFileType();
static void someStaticHelperMethod() {
//helper method implementation
}
}
What Is the Need for Default Methods?
Default methods in interfaces help us to introduce new functionality without breaking the contract of the implementing classes.
Suppose we have an Expression
interface that has ConstantExpression
, BinaryExpression
, DivisionExpression
etc. as existing implementations. Now we get a requirement to add new functionality.
Return the
signum
of the evaluated result.Return
signum
after evaluating the expression.
This can be done with default and static methods without breaking any functionality as follows.
public interface Expression {
double evaluate();
default double signum() {
return signum(evaluate());
}
static double signum(double value) {
return Math.signum(value);
}
}
You can find the full example code on GitHub.
Default Methods and Multiple Inheritance Ambiguity Problems
Java supports multiple inheritance of interfaces. Consider having two interfaces, InterfaceA
and InterfaceB
, having default methods with the same signature. Your class ConcreteC
is implementing both the interfaces.
interface InterfaceA {
void performA();
default boolean canPerform() {
// return true if I can perform the action
}
}
interface InterfaceB {
void performB();
default boolean canPerform() {
//return true if I can perform the action
}
}
class ConcreteC implements InterfaceA, InterfaceB {
}
The above code will fail to compile with "error: unrelated defaults for canPerform()
from InterfaceA
and InterfaceB
."
To overcome this problem, you need to override
the default method.
class ConcreteC implements InterfaceA, InterfaceB {
override
public boolean canPerform() {
}
}
But say you don't want to provide the implementation of the overridden default
method but instead want to reuse the existing one. That is also possible with the following syntax.
class ConcreteC implements InterfaceA, InterfaceB {
override
public boolean canPerform() {
return InterfaceA.super.canPerform();
}
}
A Few Important Points for Default Methods
You can inherit the default method.
You can redeclare the default method essentially making it
abstract
.You can redefine the default method (equivalent to overriding).
Opinions expressed by DZone contributors are their own.
Comments