Interface Enhancements
1.interface Default Methods: Java 8
-
Java 8 allows you to add non-abstract methods in interfaces. These methods must be declared default keyword.
-
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
interface Vehicle { default void move() { System.out.println("Def. move"); } } class Car implements Vehicle {} class Bus implements Vehicle {@Override public void move() { System.out.println("Bus. Move"); } } public class Test { public static void main(String[] args) { new Car().move(); new Bus().move(); } }
-
Vehicle
interface defines a methodmove()
and provided a default implementation as well. If any class implements this interface then it need not to implement its own version ofmove()
method. It can directly callinstance.move()
. -
If class willingly wants to customize the behavior of move() method then it can provide its own custom implementation and override the method by removing ‘
default
’ keyword
2.interface Static Methods: Java 8
Java interface static method is similar to default method except that we can’t override them in the implementation classes. This feature helps us in avoiding inconsistent results in case of poor implementation in implementation classes
interface Vehicle {
default void move() {
System.out.println("Def. move");
}
static void year() {
System.out.println("Def. 1998");
}
}
class Test implements Vehicle {@Override
public void move() {
System.out.println("Bus. Move");
}
static void year() {
System.out.println("2018");
}
public static void main(String[] args) {
Vehicle.year();
year();
}
}------------------
Def.1998
2018
Note that year() is a simple class method, it’s not overriding the interface method. For example, if we will add @Override annotation to the year() method, it will result in compiler error.
3.Interface Private Methods – Java 9
private methods will improve code re-usability inside interfaces. For example, if two default methods needed to share code, a private interface method would allow them to do so, but without exposing that private method to it’s implementing classes.
Using private methods in interfaces have four rules:
-
Private interface method cannot be abstract.
-
Private method can be used only inside interface.
-
Private static method can be used inside other static and non-static interface methods.
-
Private non-static methods cannot be used inside private static methods.
public interface Calculator { default int addEvenNumbers(int... nums) { return add(n -> n % 2 == 0, nums); } default int addOddNumbers(int... nums) { return add(n -> n % 2 != 0, nums); } private int add(IntPredicate predicate, int... nums) { return IntStream.of(nums) .filter(predicate) .sum(); } }
4.Functional Interfaces
Functional interfaces are also called Single Abstract Method interfaces (SAM Interfaces). As name suggest, they permit exactly one abstract method inside them.
Java 8 introduces an annotation i.e. @FunctionalInterface
which can be used for
compiler level errors when the interface you have annotated violates the
contracts of Functional Interface.
@FunctionalInterface
public interface Test {
public void firstWork();
}
Please note that a functional interface is valid even if the @FunctionalInterface annotation would be omitted. It is only for informing the compiler to enforce single abstract method inside interface.