I'm trying to learn Lambda expressions,
interface MathOperartor has operate() overloaded for types int and float, I'm sure this should be possible to do using Lambda expressions, but can't quite seem to figure out what the issue is here:
public static void main(String[] args) {
LambdaLearning lb = new LambdaLearning();
MathOperartor add = (a , b )-> a + b; // error: The target type of this expression must be a functional interface
MathOperartor sub = (a , b) -> a - b; // same error
MathOperartor mul = (a , b) -> a * b; // ''
MathOperartor div = (a , b) -> a / b; // ''
System.out.println(lb.operate(10, 15, add));
System.out.println(lb.operate(10.5f, 15.5f, sub));
System.out.println(lb.operate(10, 15, mul));
System.out.println(lb.operate(10, 15, div));
}
interface MathOperartor{
public Object operate(int a, int b);
public Object operate(float a, float b);
}
private Object operate(int a, int b, MathOperartor math){
return math.operate(a,b);
}
private Object operate(float a, float b, MathOperartor math){
return math.operate(a,b);
}
Please let me know what I'm doing wrong here and suggest a fix...
Update:
Ok, so I understood the concept of Functional Interface, My question was also about achieving what I was trying to do in the above code and I found couple of ways to do it.
Thank you every one for your valuable answers!
解决方案
A functional interface must is a SAM interface:
a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
your interface has declared 2 abstract methods that the lambda expression don't know where to going, and the lambda expression is an instance of that interface which means that must implements all the abstract methods declared in the interface. but you can adding default method to solve your problem in this case, for example:
interface MathOperartor{
//it also can be removed, since a int can cast to a float automatically
default Object operate(int a, int b){
return operate((float)a, (float)b);
}
public Object operate(float a, float b);
}