Java
8 enables us to add non-abstract method implementations to interfaces by
utilizing the default keyword. This feature is also known as Extension
Methods.
interface
Formula {
double calculate(int a);
default double sqrt(int a) {
return Math.sqrt(a);
}
}
Formula
formula = new Formula() {
@Override
public double calculate(int a) {
return sqrt(a * 100);
}
};
formula.calculate(100); // 100.0
formula.sqrt(16); // 4.0
}
No comments:
Post a Comment