Why do I need a functional Interface to work with lambdas?

前端 未结 6 2138
故里飘歌
故里飘歌 2020-11-27 15:09

I think this question is already somewhere out there, but I wasn\'t able to find it.

I don\'t understand, why it\'s necessary to have a functional interface to work

6条回答
  •  离开以前
    2020-11-27 15:42

    When you write :

    TestInterface i = () -> System.out.println("Hans");
    

    You give an implementation to the void hans() method of the TestInterface.

    If you could assign a lambda expression to an interface having more than one abstract method (i.e. a non functional interface), the lambda expression could only implement one of the methods, leaving the other methods unimplemented.

    You can't solve it by assigning two lambda expressions having different signatures to the same variable (Just like you can't assign references of two objects to a single variable and expect that variable to refer to both objects at once).

提交回复
热议问题