xxxxxxxxxx
An Interface that contains exactly one abstract method is known as a functional interface.
It can have any number of default, static methods but can contain only one abstract method.
It can also declare the methods of the object class.
Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces.
A functional interface can extend another interface only when it does not have any abstract method.
Java 8 provides predefined functional interfaces to deal with functional programming by using lambda and method references.
xxxxxxxxxx
Yes, it is possible to define our own Functional Interfaces. We use Java 8 provides the @FunctionalInterface annotation to mark an interface as a Functional Interface.
We need to follow these rules to define a Functional Interface:
Define an interface with one and only one abstract method.
We cannot define more than one abstract method.
Use @FunctionalInterface annotation in the interface definition.
We can define any number of other methods like default methods, static methods.
xxxxxxxxxx
interface Printable {
void print(String msg);
}
public class JLEExampleSingleParameter {
public static void main(String[] args) {
// with lambda expression
Printable withLambda = (msg) -> System.out.println(msg);
withLambda.print(" Print message to console....");
}
}