Functional Interface Definition
An Interface which contains only one abstract method but can have multiple default and static methods is called as Functional interface.
Functional interface is introduced in Java 8 to support Lambda expression.
We can use annotation @FunctionalInterface on top of normal interface to make it Functional interface.
Examples : Runnable ,Callable, Comparable are some of example of Functional interface in Java
In Java 8 , 4 main functional interfaces are introduced which could be used in different scenarios :Consumer, Predicate, Function , Supplier.
We can create our own Functional Interface as below :
If we write two abstract methods in Functional Interface then we get Error as :
Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample is not a functional interface
Above code is giving error because two abstract methods are there in our Functional Interface ,which is not allowed.
In next session we will understand Lambda Expression in Java 8.
Thanks For Reading
Swapnil