If an interface contains only one abstract method, then it is called as Functional Interface.
Functional Interface is used to invoke lambda expressions.
Runnable, Callable, and Comparable are predefined functional interfaces.
1. Runnable => run ( )
2. Callable => call ( )
3. Comparable => compareTo ( )
Note: We can also take default and static methods in functional interface. Only one method rule is applicable for only abstract methods means follow SAM [Single Abstract Method] rule.
Why we need functional interface ?
To invoke lambda expression we need functional interface.
To represent our interface as functional interface we will use @FunctionalInterface. Once we Annotate interface with @FunctionalInterface, compiler will verify whether this interface contain only one abstract or not if more then one abstract method found then it gives compile time error.
Let's see some example...
If you don't know about interface changes in java 8 refer my last blog https://www.miit.tech/post/interface-changes-in-java-8
Comentários