For example, writing lambda expressions for
doubleNumberFunction = public int (int x, int y){
if(y==0) return 0;
return x*y;
}
What about the type of the variable of doubleNumberFunction?? there is no Funciton type but they have a concept of Functional Interface. Now, let's leave the above function and pick up the scenario that has been discussed in the starting of this blog.
We were trying to pass a behavior in a greet method and elegant way to do was to pass the implementation of an interface that holds the particular greeting. For e.g. we have different greetings: Birthdays, Wedding Annivesaries, Brand Annivesaries e.t.c. Let's say, all of them will have a greet message for now and will respond particular message that means, all of the implementation classes will have one method implementation and the type interface will have one abstract method.
interface Greet{
public void greet();
}
public class Birthday implements Greet{
public void greet(){
System.out.println("Happy Birthday");
}
}
and so on with the other implementations. Here comes the Functional Interface into the picture, in java 8 Functional interface has been introduced, which is simply define as an interface which have only one abstract method. As in java 8 we can have default methods as to give default implementations but to be a functional interface it should have one abstact method it can have any number of defauilt methods but one abstract method.
The type of a lambda expression holder needs to be of Functional interface. To make an interface Functional we just need to add an annotation @FunctionalInterface like this:
@FunctionalInterface
interface Greet{
public void greet();
}
Now we can have the implementations/function write as lambda expression and directly pass into the Functional interface type holder.
Greet lambdaGreet = () -> System.out.println("Happy Birthday");
This looks similar to anonymous inner class but it is different, lambdas and anonymous inner classes are different things.
References: javabrains.io
doubleNumberFunction = public int (int x, int y){
if(y==0) return 0;
return x*y;
}
What about the type of the variable of doubleNumberFunction?? there is no Funciton type but they have a concept of Functional Interface. Now, let's leave the above function and pick up the scenario that has been discussed in the starting of this blog.
We were trying to pass a behavior in a greet method and elegant way to do was to pass the implementation of an interface that holds the particular greeting. For e.g. we have different greetings: Birthdays, Wedding Annivesaries, Brand Annivesaries e.t.c. Let's say, all of them will have a greet message for now and will respond particular message that means, all of the implementation classes will have one method implementation and the type interface will have one abstract method.
interface Greet{
public void greet();
}
public class Birthday implements Greet{
public void greet(){
System.out.println("Happy Birthday");
}
}
and so on with the other implementations. Here comes the Functional Interface into the picture, in java 8 Functional interface has been introduced, which is simply define as an interface which have only one abstract method. As in java 8 we can have default methods as to give default implementations but to be a functional interface it should have one abstact method it can have any number of defauilt methods but one abstract method.
The type of a lambda expression holder needs to be of Functional interface. To make an interface Functional we just need to add an annotation @FunctionalInterface like this:
@FunctionalInterface
interface Greet{
public void greet();
}
Now we can have the implementations/function write as lambda expression and directly pass into the Functional interface type holder.
Greet lambdaGreet = () -> System.out.println("Happy Birthday");
This looks similar to anonymous inner class but it is different, lambdas and anonymous inner classes are different things.
References: javabrains.io
No comments:
Post a Comment