Functional interface→
Functional interface are powerful tool in Java for implementing functional programming
techniques
It is an interface with exactly 1 abstract method, enabling a to be used with lamda
expression , and method references.
Keypoints→
1st)--> it must have only one abstract method that is why it also called single abstract
method interface ( SAM )
2)--> it allow lemda expression to making the code redable
3)--> it can also be implemented using method references for simplifying the code
4)--> they can also have multiple default and static method
How to use functional interface
1)--> create an interface with exactly one abstract method
2)--> use lemda expression to provide the implementation of the abstract method
3)--> use method references to provide the implementation of the abstract method
Eg-
@FunctionalInterface
interface MyName {
void printName();
}
public class Main {
public static void main(String[] args) {
// Lambda expression
MyName name = () -> System.out.println("My name is Ankit");
// Calling the method
name.printName();
}
}
Output: My name is Ankit