7
@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;
takendarkk
  • 3,347
  • 8
  • 25
  • 37
sairam
  • 160
  • 1
  • 1
  • 10
  • 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. – Michael Aug 02 '17 at 16:46
  • 1
    What would happen when you called `ml1.apply2(x, y)`? – Louis Wasserman Aug 02 '17 at 16:48
  • thank you all for the comments. I think i got the answer, will post answer – sairam Aug 02 '17 at 17:07

3 Answers3

4

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.

Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
  • 1
    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 Aug 02 '17 at 17:05
  • @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 :) – Grzegorz Piwowarek Aug 02 '17 at 17:12
  • that makes sense. but please check my answer why it is mandatory to have only one method :) thanks for the help – sairam Aug 02 '17 at 17:16
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.

sairam
  • 160
  • 1
  • 1
  • 10
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

Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
mgyongyosi
  • 2,557
  • 2
  • 13
  • 20
  • 4
    This answers "what is a functional interface", but not "why can't functional interfaces contain more than one abstract method". – Michael Aug 02 '17 at 16:41
  • thanks for the answer, understand that it is following single function contract but why? – sairam Aug 02 '17 at 17:03
  • 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. – kaushalpranav Apr 16 '20 at 02:21