Functional Interface -> First of all, Let's Understand the concept of @FuntionalInterface
as we know that JDK-1.8
One New Concept Came known as Lambda Expersion :
Lambda Expersion
You need to understand first concept about LamdaExpersion then you can easily Get What is Funcation Interface role..
// Java program to demonstrate lambda expressions
// to implement a user defined functional interface.
// A sample functional interface (An interface with
// single abstract method
interface FuncInterface
{
// An abstract function
void abstractFun(int x);
// A non-abstract (or default) function
default void normalFun()
{
System.out.println("Hello");
}
}
class Test
{
public static void main(String args[])
{
// lambda expression to implement above
// functional interface. This interface
// by default implements abstractFun()
FuncInterface fobj = (int x)->System.out.println(2*x);
// This calls above lambda expression and prints 10.
fobj.abstractFun(5);
}
}
Lambda Expersion work only if your Define Interface have only 1 Method define because java compiler understand only if there is only one method in interface , then you dont need to denfine that method in Your Class, therefore : Anotation comes in picture that, when you want implement Lambda Expersion then use have to use @FunctionInterface
in your Interface. If you, by mistake, write one more method in Your Interface, then Compiler will tell you that this is Functional Interface. Only One method will have to define to use Lamda Experssion in your Application
@FunctionalInterface
interface sayable{
void say(String msg);
}
public class FunctionalInterfaceExample implements sayable{
public void say(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
FunctionalInterfaceExample fie = new FunctionalInterfaceExample();
fie.say("Hello there");
}
}