Java method can't be applied with Lambda expression

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
9
down vote

favorite
1












I've watched and read https://caveofprogramming.com/java/whats-new-in-java-8-lambda-expressions.html and I follow the same pattern I did for runner object which works fine.



Runner runner = new Runner();
runner.run(() -> System.out.println("Print from Lambda expression"));


Then, I try to create a simple interface and class to apply what I learned. I just want to replace the anonymous class with a lambda expression. My understanding is a lambda expression is a shorter code for the anonymous class and improve readability.



So, I tried to initiate another instance called eucalyptus1 and try to @Override the grow() method, but my IDE error message said:




grow() in com.smith.Eucalyptus cannot be applied to (lambda expression)




Could anyone point me out what I misunderstand here?



The code is below:



// a simple interface
interface Plant
public void grow();


// apply interface to class
class Eucalyptus implements Plant
@Override
public void grow()
System.out.println("This is from Eucalyptus");



public class Main
public static void main(String args)

// Create an instance of Eucalyptus
Eucalyptus eucalyptus = new Eucalyptus();
eucalyptus.grow();

// Anonymous class Myrtle from Plant interface
Plant myrtle = new Plant()
@Override
public void grow()
System.out.println("This was running from anonymous class from Plant Interface");

;

myrtle.grow();

// Try to create a lambda expression from Plant interface
// and override grow() method
// by print ("This was running from Lambda expression")

// this won't work. why?
Eucalyptus eucalyptus1 = new Eucalyptus();
eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));











share|improve this question



























    up vote
    9
    down vote

    favorite
    1












    I've watched and read https://caveofprogramming.com/java/whats-new-in-java-8-lambda-expressions.html and I follow the same pattern I did for runner object which works fine.



    Runner runner = new Runner();
    runner.run(() -> System.out.println("Print from Lambda expression"));


    Then, I try to create a simple interface and class to apply what I learned. I just want to replace the anonymous class with a lambda expression. My understanding is a lambda expression is a shorter code for the anonymous class and improve readability.



    So, I tried to initiate another instance called eucalyptus1 and try to @Override the grow() method, but my IDE error message said:




    grow() in com.smith.Eucalyptus cannot be applied to (lambda expression)




    Could anyone point me out what I misunderstand here?



    The code is below:



    // a simple interface
    interface Plant
    public void grow();


    // apply interface to class
    class Eucalyptus implements Plant
    @Override
    public void grow()
    System.out.println("This is from Eucalyptus");



    public class Main
    public static void main(String args)

    // Create an instance of Eucalyptus
    Eucalyptus eucalyptus = new Eucalyptus();
    eucalyptus.grow();

    // Anonymous class Myrtle from Plant interface
    Plant myrtle = new Plant()
    @Override
    public void grow()
    System.out.println("This was running from anonymous class from Plant Interface");

    ;

    myrtle.grow();

    // Try to create a lambda expression from Plant interface
    // and override grow() method
    // by print ("This was running from Lambda expression")

    // this won't work. why?
    Eucalyptus eucalyptus1 = new Eucalyptus();
    eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));











    share|improve this question

























      up vote
      9
      down vote

      favorite
      1









      up vote
      9
      down vote

      favorite
      1






      1





      I've watched and read https://caveofprogramming.com/java/whats-new-in-java-8-lambda-expressions.html and I follow the same pattern I did for runner object which works fine.



      Runner runner = new Runner();
      runner.run(() -> System.out.println("Print from Lambda expression"));


      Then, I try to create a simple interface and class to apply what I learned. I just want to replace the anonymous class with a lambda expression. My understanding is a lambda expression is a shorter code for the anonymous class and improve readability.



      So, I tried to initiate another instance called eucalyptus1 and try to @Override the grow() method, but my IDE error message said:




      grow() in com.smith.Eucalyptus cannot be applied to (lambda expression)




      Could anyone point me out what I misunderstand here?



      The code is below:



      // a simple interface
      interface Plant
      public void grow();


      // apply interface to class
      class Eucalyptus implements Plant
      @Override
      public void grow()
      System.out.println("This is from Eucalyptus");



      public class Main
      public static void main(String args)

      // Create an instance of Eucalyptus
      Eucalyptus eucalyptus = new Eucalyptus();
      eucalyptus.grow();

      // Anonymous class Myrtle from Plant interface
      Plant myrtle = new Plant()
      @Override
      public void grow()
      System.out.println("This was running from anonymous class from Plant Interface");

      ;

      myrtle.grow();

      // Try to create a lambda expression from Plant interface
      // and override grow() method
      // by print ("This was running from Lambda expression")

      // this won't work. why?
      Eucalyptus eucalyptus1 = new Eucalyptus();
      eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));











      share|improve this question















      I've watched and read https://caveofprogramming.com/java/whats-new-in-java-8-lambda-expressions.html and I follow the same pattern I did for runner object which works fine.



      Runner runner = new Runner();
      runner.run(() -> System.out.println("Print from Lambda expression"));


      Then, I try to create a simple interface and class to apply what I learned. I just want to replace the anonymous class with a lambda expression. My understanding is a lambda expression is a shorter code for the anonymous class and improve readability.



      So, I tried to initiate another instance called eucalyptus1 and try to @Override the grow() method, but my IDE error message said:




      grow() in com.smith.Eucalyptus cannot be applied to (lambda expression)




      Could anyone point me out what I misunderstand here?



      The code is below:



      // a simple interface
      interface Plant
      public void grow();


      // apply interface to class
      class Eucalyptus implements Plant
      @Override
      public void grow()
      System.out.println("This is from Eucalyptus");



      public class Main
      public static void main(String args)

      // Create an instance of Eucalyptus
      Eucalyptus eucalyptus = new Eucalyptus();
      eucalyptus.grow();

      // Anonymous class Myrtle from Plant interface
      Plant myrtle = new Plant()
      @Override
      public void grow()
      System.out.println("This was running from anonymous class from Plant Interface");

      ;

      myrtle.grow();

      // Try to create a lambda expression from Plant interface
      // and override grow() method
      // by print ("This was running from Lambda expression")

      // this won't work. why?
      Eucalyptus eucalyptus1 = new Eucalyptus();
      eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));








      java lambda java-8 override functional-interface






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 10 at 11:14









      Andrew Tobilko

      21.2k83875




      21.2k83875










      asked Sep 10 at 10:11









      Smith Lo

      967




      967






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          The difference there is that you're trying to override the implementation of Eucalyptus which is a class that implement the interface.



          Eucalyptus eucalyptus1 = new Eucalyptus();
          eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));
          ^__ // you cannot override a method using an instance of a class which is just an implementation of the interface


          All you end up doing there is pass a lambda parameter and of course, a method without arguments in its definition wouldn't compile if supplied with one at the time of method call.




          Instead, you can compare the way of implementing the lambda as :



          //Anonymous class Myrtle from Plant interface
          Plant myrtle = new Plant()
          @Override
          public void grow()
          System.out.println("This was running from anonymous class from Plant Interface");

          ;
          myrtle.grow();


          can be represented as a lambda representation:



          Plant lambdaRep = () -> System.out.println("This is running via lambda from Plant Interface");
          lambdaRep.grow();





          share|improve this answer





























            up vote
            10
            down vote













            Problem



            The method grow doesn't take any parameters, so you got the compilation error.



            Explanation



            The lambda () -> System.out.println("This from Lambda expression") itself can represent a Plant (not Eucalyptus*) instance:



            Plant plant = () -> System.out.println("This from Lambda expression");



            Try to create a lambda expression from Plant interface and override grow() method by print "This was running from Lambda expression".




            There is a slight misunderstanding here. A lambda isn't supposed to override a method, it's up to provide a method based on a @FunctionalInterface type.




            *If you were to define an Eucalyptus object by a lambda, it would be ambiguous and unclear what method the lambda would represent. Therefore, it's prohibited (even for abstract classes with a single abstract method.)






            share|improve this answer





























              up vote
              7
              down vote













              Your use of lambda expression is incorrect here.



              You use lambda expressions to implement an interface. In this case, you would be providing an implementation of Plant using a lambda expression, rather than calling that interface's method with a lambda expression:



              Here's a normal use:



              Plant eucalyptus1 = () -> System.out.println("This from Lambda expression");
              eucalyptus1.grow(); // Prints "This from Lambda expression"


              In other words, because you're having a functional interface, you should be able to avoid creating classes implementing it (anonymous or not).



              So you should not need to create the Eucalyptus class at all.






              share|improve this answer




















                Your Answer





                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52255510%2fjava-method-cant-be-applied-with-lambda-expression%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                10
                down vote



                accepted










                The difference there is that you're trying to override the implementation of Eucalyptus which is a class that implement the interface.



                Eucalyptus eucalyptus1 = new Eucalyptus();
                eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));
                ^__ // you cannot override a method using an instance of a class which is just an implementation of the interface


                All you end up doing there is pass a lambda parameter and of course, a method without arguments in its definition wouldn't compile if supplied with one at the time of method call.




                Instead, you can compare the way of implementing the lambda as :



                //Anonymous class Myrtle from Plant interface
                Plant myrtle = new Plant()
                @Override
                public void grow()
                System.out.println("This was running from anonymous class from Plant Interface");

                ;
                myrtle.grow();


                can be represented as a lambda representation:



                Plant lambdaRep = () -> System.out.println("This is running via lambda from Plant Interface");
                lambdaRep.grow();





                share|improve this answer


























                  up vote
                  10
                  down vote



                  accepted










                  The difference there is that you're trying to override the implementation of Eucalyptus which is a class that implement the interface.



                  Eucalyptus eucalyptus1 = new Eucalyptus();
                  eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));
                  ^__ // you cannot override a method using an instance of a class which is just an implementation of the interface


                  All you end up doing there is pass a lambda parameter and of course, a method without arguments in its definition wouldn't compile if supplied with one at the time of method call.




                  Instead, you can compare the way of implementing the lambda as :



                  //Anonymous class Myrtle from Plant interface
                  Plant myrtle = new Plant()
                  @Override
                  public void grow()
                  System.out.println("This was running from anonymous class from Plant Interface");

                  ;
                  myrtle.grow();


                  can be represented as a lambda representation:



                  Plant lambdaRep = () -> System.out.println("This is running via lambda from Plant Interface");
                  lambdaRep.grow();





                  share|improve this answer
























                    up vote
                    10
                    down vote



                    accepted







                    up vote
                    10
                    down vote



                    accepted






                    The difference there is that you're trying to override the implementation of Eucalyptus which is a class that implement the interface.



                    Eucalyptus eucalyptus1 = new Eucalyptus();
                    eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));
                    ^__ // you cannot override a method using an instance of a class which is just an implementation of the interface


                    All you end up doing there is pass a lambda parameter and of course, a method without arguments in its definition wouldn't compile if supplied with one at the time of method call.




                    Instead, you can compare the way of implementing the lambda as :



                    //Anonymous class Myrtle from Plant interface
                    Plant myrtle = new Plant()
                    @Override
                    public void grow()
                    System.out.println("This was running from anonymous class from Plant Interface");

                    ;
                    myrtle.grow();


                    can be represented as a lambda representation:



                    Plant lambdaRep = () -> System.out.println("This is running via lambda from Plant Interface");
                    lambdaRep.grow();





                    share|improve this answer














                    The difference there is that you're trying to override the implementation of Eucalyptus which is a class that implement the interface.



                    Eucalyptus eucalyptus1 = new Eucalyptus();
                    eucalyptus1.grow(() -> System.out.println("This from Lambda expression"));
                    ^__ // you cannot override a method using an instance of a class which is just an implementation of the interface


                    All you end up doing there is pass a lambda parameter and of course, a method without arguments in its definition wouldn't compile if supplied with one at the time of method call.




                    Instead, you can compare the way of implementing the lambda as :



                    //Anonymous class Myrtle from Plant interface
                    Plant myrtle = new Plant()
                    @Override
                    public void grow()
                    System.out.println("This was running from anonymous class from Plant Interface");

                    ;
                    myrtle.grow();


                    can be represented as a lambda representation:



                    Plant lambdaRep = () -> System.out.println("This is running via lambda from Plant Interface");
                    lambdaRep.grow();






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 10 at 10:23

























                    answered Sep 10 at 10:16









                    nullpointer

                    30.9k1056129




                    30.9k1056129






















                        up vote
                        10
                        down vote













                        Problem



                        The method grow doesn't take any parameters, so you got the compilation error.



                        Explanation



                        The lambda () -> System.out.println("This from Lambda expression") itself can represent a Plant (not Eucalyptus*) instance:



                        Plant plant = () -> System.out.println("This from Lambda expression");



                        Try to create a lambda expression from Plant interface and override grow() method by print "This was running from Lambda expression".




                        There is a slight misunderstanding here. A lambda isn't supposed to override a method, it's up to provide a method based on a @FunctionalInterface type.




                        *If you were to define an Eucalyptus object by a lambda, it would be ambiguous and unclear what method the lambda would represent. Therefore, it's prohibited (even for abstract classes with a single abstract method.)






                        share|improve this answer


























                          up vote
                          10
                          down vote













                          Problem



                          The method grow doesn't take any parameters, so you got the compilation error.



                          Explanation



                          The lambda () -> System.out.println("This from Lambda expression") itself can represent a Plant (not Eucalyptus*) instance:



                          Plant plant = () -> System.out.println("This from Lambda expression");



                          Try to create a lambda expression from Plant interface and override grow() method by print "This was running from Lambda expression".




                          There is a slight misunderstanding here. A lambda isn't supposed to override a method, it's up to provide a method based on a @FunctionalInterface type.




                          *If you were to define an Eucalyptus object by a lambda, it would be ambiguous and unclear what method the lambda would represent. Therefore, it's prohibited (even for abstract classes with a single abstract method.)






                          share|improve this answer
























                            up vote
                            10
                            down vote










                            up vote
                            10
                            down vote









                            Problem



                            The method grow doesn't take any parameters, so you got the compilation error.



                            Explanation



                            The lambda () -> System.out.println("This from Lambda expression") itself can represent a Plant (not Eucalyptus*) instance:



                            Plant plant = () -> System.out.println("This from Lambda expression");



                            Try to create a lambda expression from Plant interface and override grow() method by print "This was running from Lambda expression".




                            There is a slight misunderstanding here. A lambda isn't supposed to override a method, it's up to provide a method based on a @FunctionalInterface type.




                            *If you were to define an Eucalyptus object by a lambda, it would be ambiguous and unclear what method the lambda would represent. Therefore, it's prohibited (even for abstract classes with a single abstract method.)






                            share|improve this answer














                            Problem



                            The method grow doesn't take any parameters, so you got the compilation error.



                            Explanation



                            The lambda () -> System.out.println("This from Lambda expression") itself can represent a Plant (not Eucalyptus*) instance:



                            Plant plant = () -> System.out.println("This from Lambda expression");



                            Try to create a lambda expression from Plant interface and override grow() method by print "This was running from Lambda expression".




                            There is a slight misunderstanding here. A lambda isn't supposed to override a method, it's up to provide a method based on a @FunctionalInterface type.




                            *If you were to define an Eucalyptus object by a lambda, it would be ambiguous and unclear what method the lambda would represent. Therefore, it's prohibited (even for abstract classes with a single abstract method.)







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Sep 10 at 10:39

























                            answered Sep 10 at 10:16









                            Andrew Tobilko

                            21.2k83875




                            21.2k83875




















                                up vote
                                7
                                down vote













                                Your use of lambda expression is incorrect here.



                                You use lambda expressions to implement an interface. In this case, you would be providing an implementation of Plant using a lambda expression, rather than calling that interface's method with a lambda expression:



                                Here's a normal use:



                                Plant eucalyptus1 = () -> System.out.println("This from Lambda expression");
                                eucalyptus1.grow(); // Prints "This from Lambda expression"


                                In other words, because you're having a functional interface, you should be able to avoid creating classes implementing it (anonymous or not).



                                So you should not need to create the Eucalyptus class at all.






                                share|improve this answer
























                                  up vote
                                  7
                                  down vote













                                  Your use of lambda expression is incorrect here.



                                  You use lambda expressions to implement an interface. In this case, you would be providing an implementation of Plant using a lambda expression, rather than calling that interface's method with a lambda expression:



                                  Here's a normal use:



                                  Plant eucalyptus1 = () -> System.out.println("This from Lambda expression");
                                  eucalyptus1.grow(); // Prints "This from Lambda expression"


                                  In other words, because you're having a functional interface, you should be able to avoid creating classes implementing it (anonymous or not).



                                  So you should not need to create the Eucalyptus class at all.






                                  share|improve this answer






















                                    up vote
                                    7
                                    down vote










                                    up vote
                                    7
                                    down vote









                                    Your use of lambda expression is incorrect here.



                                    You use lambda expressions to implement an interface. In this case, you would be providing an implementation of Plant using a lambda expression, rather than calling that interface's method with a lambda expression:



                                    Here's a normal use:



                                    Plant eucalyptus1 = () -> System.out.println("This from Lambda expression");
                                    eucalyptus1.grow(); // Prints "This from Lambda expression"


                                    In other words, because you're having a functional interface, you should be able to avoid creating classes implementing it (anonymous or not).



                                    So you should not need to create the Eucalyptus class at all.






                                    share|improve this answer












                                    Your use of lambda expression is incorrect here.



                                    You use lambda expressions to implement an interface. In this case, you would be providing an implementation of Plant using a lambda expression, rather than calling that interface's method with a lambda expression:



                                    Here's a normal use:



                                    Plant eucalyptus1 = () -> System.out.println("This from Lambda expression");
                                    eucalyptus1.grow(); // Prints "This from Lambda expression"


                                    In other words, because you're having a functional interface, you should be able to avoid creating classes implementing it (anonymous or not).



                                    So you should not need to create the Eucalyptus class at all.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Sep 10 at 10:18









                                    ernest_k

                                    14.6k41429




                                    14.6k41429



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52255510%2fjava-method-cant-be-applied-with-lambda-expression%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        這個網誌中的熱門文章

                                        Why am i infinitely getting the same tweet with the Twitter Search API?

                                        Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

                                        Strongly p-embedded subgroups and p-Sylow subgroups.