An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default, static methods but can contain only one abstract method.
Functional Interface also known as Single Abstract Method Interfaces or SAM Interfaces. It is a new feature in Java 8, which helps to achieve functional programming approach.
The java.util.function package contains general purpose functional interfaces used by JDK and also available for end users like us. The interfaces defined in this package are annotated with @FunctionalInterface.
Since java 1.8 onward below interfaces are functional interface, all these interfaces annotated with @FunctionalInterface.
1. Runnable –> It contains only run() method
2. Comparable –> It contains only compareTo() method
3. ActionListener –> It contains only actionPerformed()
4. Callable –> It contains only call() method
Below are few important Functional Interfaces in java.util.function package.
Represents a predicate (boolean-valued function) of one argument.
Represents a supplier of results.
Represents a function that accepts one argument and produces a result.
Represents an operation that accepts a single input argument and returns no result.
Represents an operation that accepts two input arguments and returns no result.
Represents a function that accepts two arguments and produces a result.
Represents a predicate (boolean-valued function) of two arguments.
@FunctionalInterface annotation :
This annotation will be used to define Functional interface. This annotation is not the requirement for the java compiler to determine the interface is an functional interface but it helps the compiler to identify the accidental violation of the our design intent.
This annotation will be very much useful for us while creating our custom functional interfaces. @FunctionalInterface is used to verify the interface follows all of the rules that can make this interface as functional interface.
We can create functional interface without using @FunctionalInterface, but if we define multipleabstract methods compiler won’t throw any error while creating interface. If you define interface using @FunctionalInterface, it ensures the interface should contain single abstract method.
@FunctionalInterface Rules :
Inside Functional Interface we can take only one abstract method, if we take more than one abstract method then compiler raise an error.
Interface can declares an abstract method overriding one of the public method from java.lang.Object, that still can be considered as functional interface. The reason is any implementation class to this interface will have implementation for this abstract method either from super class (bare minimum java.lang.Object) class or defined by implementation class it self. In the below example toString() method declared as abstract which will be implemented in its concrete implementation class or at last derived from java.lang.Object class.
System.out.println(" Default Method : allowed since java 1.8 ");
System.out.println("Static Method : allowed since java 1.8 ");
3. Functional Interface with respect to Inheritance :
If an interface extends Functional Interface and child interface doesn’t contain any abstract method
then child interface is also Functional Interface. In this case In child interface we can’t define any new abstract methods
In the child interface we can define exactly same parent interface abstract method.