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!