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);
}
Generally the other answers are right, that a @FunctionalInterface
is one that has a single abstract method, besides those that are declared in Object
. This is still legal:
@FunctionalInterface
static interface Testable {
public abstract String toString();
public abstract void test();
}
The target for lambda in the functional interface which is also known as SAM (Single Abstract Method) Interface.
Following is how you can run your example:
@FunctionalInterface
public interface MathOperator<T> {
T operate(T a,T b);
}
public class MathOperations {
public static void main(String[] args) {
MathOperator<Integer> add = (a, b) -> a + b;
MathOperator<Integer> sub = (a, b) -> a - b;
System.out.println(add.operate(1, 2));
System.out.println(sub.operate(2, 1));
}
}
Hope it will help you
Functional Interface can have a single abstract method not more than that.
An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface.
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.
Example:
@FunctionalInterface
public interface SimpleFuncInterface {
public void doWork();
}
But an interface can have one abstract method but any number of default methods and the interface would still be called an functional interface.
Example:
So, your code will be converted to:
interface MathOperartor {
default Object operate(int a, int b) {
return operate((float) a, (float) b);
}
public Object operate(float a, float b);
}
Instances of functional interfaces can be created with lambda expressions, method references, or constructor references.
However, the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.
Basically, your interface can have only one method. You can't overload them
- What is use of Functional Interface
- Oracle docs
- short dzone tutorial
- Oracle lambda expressions