@FunctionalInterface
interface MyLambda {
void apply1();
int apply2(int x, int y);
}
Now using the Lambda expressions why can't Java allow below two as it clearly differentiate between the two:
MyLambda ml1 = () -> System.out.println("Hello");
MyLambda ml2 = (x, y) -> x+y;
The answer would be that in order to create a valid implementation, you would need to be able to pass N lambdas at once and this would introduce a lot of ambiguity and a huge decrease in readability.
The other thing is that @FunctionalInterface
is used to denote an interface which can be used as a target for a lambda expression and lambda is a SINGLE function.
Anyway, your example is not valid and would not compile because it tries to create two incomplete implementations on the functional interface.
Writing Lamba expression meaning we are implementing the interface that is functional interface. It should have one abstract method because at the time of lambda expression, we can provide only one implementation at once. So in the code snippet posted in the question, at any time we are giving only one implementation while declaring Lambda where we will have to implement for two abstract methods.
Thanks for the help.
A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. This "single" method may take the form of multiple abstract methods with override-equivalent signatures inherited from superinterfaces; in this case, the inherited methods logically represent a single method.
https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With