Definition:
@FunctionalInterface annotation is useful for compilation time checking of your code.You cannot have more than one method besides
static,
default and
abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.
Insight 1:
Conceptually,a functional interface has exactly one abstract method.
Since default methods have an implementation, they are not abstract.
Since default methods are not abstract you’re free to add default methods to your functional interface as many as you like.
Insight 2:
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.Insight 3:
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.e.g. Below is a valid functional interface even though it declared two abstract methods. Why? Because one of these abstract methods “equals()” which has signature equal to public method in Object class.
@FunctionalInterface
interface MathOperation {
int operation(int a, int b);
@Override
public String toString(); //Overridden from Object class
@Override
public boolean equals(Object obj); //Overridden from Object class
}
While the intended use of Functional interfaces is for lambda expressions, method references and constructor references, they can still be used, like any interface, with anonymous classes, implemented by classes, or created by factory methods.
0 comments:
Post a Comment