2

I am currently reading Java 8 Lambda expressions. I learnt that,

  • Lambda expressions can operate only on functional interfaces (i.e. interface with only one abstract method).

I also learnt that,

  • Signature of lambda expression is decided by signature of that one abstract method.

My question is, if we have 2 methods in an interface which are different in types of input parameters and no. of input parameters, why can't lambda expression deduce its signature from either of the method ? After all, its just need to find type of input parameters of the function, isn't it ? Why the restriction of only one method is put by Java 8 creators ?

Edit: The answer at this link as commented by Ousmane Mahy Diaw does not really answer, why 2 methods in the functional inteface are not allowed. The answer just say, its a requirement of Lambda expression to have functional interface only one method.

Community
  • 1
  • 1
Shailesh Pratapwar
  • 4,054
  • 3
  • 35
  • 46
  • Perhaps you should explain what you're asking with some code samples. – pvg May 04 '17 at 17:48
  • 1
    What do you think should happen when the other method is called? – shmosel May 04 '17 at 17:53
  • Well, in lambda we can write the code to execute when other method is called. Its just a matter of which method will be called depending on types or number of input parameters. Isn't it ? – Shailesh Pratapwar May 04 '17 at 17:56
  • 1
    Suppose you have an `interface Foo{void foo(int i); void bar(String s);}`. You have a method that takes this interface as argument: `void doSomething(Foo f){f.foo(0);f.bar("");}` You call the method with a lambda: `doSomething((int i)->{});` Now what? –  May 04 '17 at 18:01
  • Also, suppose you have two signatures: `String foo(Integer i)` and `String foo(String s)` with a lambda expression `Object::toString`. Which method does that implement? It could be either... – shmosel May 04 '17 at 18:08
  • 5
    Even assuming you could come up with some syntax to support multiple methods, and it wasn't very similar to anonymous classes, you would have to take that to the language designers. Questions of the nature of "why doesn't [language] support [feature]?" are considered off-topic here, because answering them would involve opinion and speculation. – shmosel May 04 '17 at 18:12
  • @Arkadiy In this case, Lambda needs to enforce that all methods in an interface must be implemented. Don't see any downside of such enforcement. If anyone has a need for more than 1 method in an interface, the implementation will be provided. If anyone feels sloppy implementing all methods, they can go with creating only one method per interface. What do you think ? – Shailesh Pratapwar May 06 '17 at 02:44
  • @shmosel Object::toString will call the toString method as its a method reference. And, afterall we know which method is there and so we will not call any method which is not there. – Shailesh Pratapwar May 06 '17 at 02:58
  • @shmosel Ok. I agree. So, just for conclusion, I would say that it was possible but because of some reasons language designers didn't allow it. – Shailesh Pratapwar May 06 '17 at 03:00
  • There seems to be a common misconception. It’s tempting to see a lambda expressions as a way of implementing an interface, as that’s what happens technically (in Java), which causes a bunch of typical questions, like “Why only interfaces?” or “Why not interfaces with multiple abstract methods?”. But this is missing the point. Lambda expressions are a concept, existing in a lot of programming languages, for describing small anonymous *functions*. It’s a minor technical detail that Java’s way of integrating them into the existing type system uses functional interfaces, not the actual goal… – Holger May 23 '17 at 13:22

0 Answers0