8
@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;
3
  • There's no technical reason why you couldn't. It wouldn't look like that, though. The syntax would likely be horrible, possibly to the detriment of single method lambdas. It's also not particularly useful. The more methods you added, the more compelling it becomes to write a proper implementing class. Commented Aug 2, 2017 at 16:46
  • 1
    What would happen when you called ml1.apply2(x, y)? Commented Aug 2, 2017 at 16:48
  • thank you all for the comments. I think i got the answer, will post answer Commented Aug 2, 2017 at 17:07

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer, is that just because of readability thing. we have the concept of overloading which we can have for Lambdas or abstract methods is what I am thinking !!
@sairam the more method you put in one interface, the more ambiguity you would get if this was possible - and we already have a nice tool for doing this - classic interface implementations :)
that makes sense. but please check my answer why it is mandatory to have only one method :) thanks for the help
2

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.

Comments

1

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

3 Comments

This answers "what is a functional interface", but not "why can't functional interfaces contain more than one abstract method".
thanks for the answer, understand that it is following single function contract but why?
The reason for having only one function is - Java wanted to provide capabilities for functional programming like in JavaScript where functions are objects too and can be passed as parameters. In order to provide that capability, they included @FunctionalInterface, so that there is only one function in that class. And when you pass the class as a parameter, viola ! you are passing the functionality like in JavaScript.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.