The word inference means a conclusion that has been reach on the basis of evidence and reasoning. Here, the type is the evidence that can be use to reach a conclusion of lambda expression. For e.g. in our class where different greetings need to be implemented and we have define one interface Greet for it. Let's add a parameter into it for more understanding.
@FunctionalInterface
interface Greet{
public void greetMessage(String name);
}
when we are using Greet type object to pass around the method as a lambda expression holder and particular name for print with that message:
public GreetRunner{
public static void printGreet(String name, Greet greet){
greet.greetMessage(name);
}
public static void main(String args[]){
Greet lambdaGreet = (String name) -> System.out.println("Happy Birthday"+name);
printGreet("Fahad",lambdaGreet);
}
}
Greet lambdaGreet = (String name) -> System.out.println("Happy Birthday"+name);
Here we can use directly the lambda expression, instead of just passing the variable we can pass the lambda expression. Furthermore, we can omit the type String from the parameter since the Type of the lambda or the functional interface method has defined that it is a String Or in other words the interface is infering the lambda expression by providing all the reasoning and evidences from the interface method definition.
printGreet("Fahad",(name) -> System.out.println("Happy Birthday"+name));
One more thing is that, we can actually remove the round brackets if there is only one parameter. Therefore, the last simplified version would be look like this:
printGreet("Fahad",name -> System.out.println("Happy Birthday"+name));
A Reason for Functional Interfaces
Why they have not created new Function type or Lambda Type the reason is to make it flexible in terms of compatibility. If they have created a Lambda type then all the lambda expression will be valid for that particular type only. In case of new function type you have to write another lambda type and change the implementation.
Advantage of using interface is that you can use lambdas in the place of all the anonymous inner classes and methods which accepts the interface you do not have to rewrite for a new function type. For e.g in Runnable there is one method "run" in jave 8 it has been declared as Functional interface. We can use the lambda expression instead of write the anonymous inner class to make the object of thread.
As for java 8 we can add implementation to the interfaces as default methods. To be use an interface for lambda expression that interface needs to have only one abstract method, it can have multiple default methods but one abstract method. Though the Java compiler will not complain if you do not define @FunctionalInterface annotation at the interface definition but it is a good practice to put on it, therefore, other programmers know the purpose of the interface. However, if you add another abstract method while the annotation is there it will show compile time error.
References: javabrains.io
No comments:
Post a Comment