Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
6/24/2014 How to tell Java to use a custom functional interface ... | Java.net Login The Source for Java Technology Collaboration Home Projects Forums People Java User Groups Forums Blogs Konstantin Yovkov Adopt a JSR | M ain | Create a Project Link an Offsite Project Blogs Events Java Magazine Oracle University People Search All Recent Entries About Java.net Articles Help JCP Get Involved Get Informed Projects Join Now How to tell Java to apply a lambda expressions for a custom functional interface instead for the one from the method's contract? How to tell Java to apply a lambda expressions for a custom functional interface instead for the one from the method's contract? Archives June 2014 May 2014 Posted by koc ko on June 19, 2014 at 10:11 PM PDT The lambdas are without any doubt one of the most intriguing and attractive features in Java8, but sometimes instead of helping us writting a better and boilerplateless code, they can get us into trouble. And still, they are the better alternative to the anonymous classes for lots of reasons. The anonymous classes were a nice way to achieve clojures in Java, but it was something natural to write a lot of boilerplate code to achive something atomic. Let take a look on the following code snippet: myButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(“Button clicked”); } }); It does a single thing - writing a message on the standart output, but in order to have it implemented, we wrote five lines of code. Which is not really cool and is a sign for a boilerplate code. The same statement, but written with the terms of lambdas, would look like: myButton.addActionListener(e -> System.out.printn(“Button clicked”)); This looks pretty neat and more readable, isn't it ? :-) But, there is, however, a problem with the lambda approach! Let's first get back to the anonymous class and write our own implemnetation of the java.awt.event.ActionListener interface, which has a member variable within: public abstract class MyActionListener extends ActionListener { protected int fourtyTwo = 42; public abstract void actionPerformed(ActionEvent event); } Using anonymous classes, it won't be a problem to create an anonymous implementation of M yActionListener and even to refer the nester member within the implementaion: myButton.addActionListener(new MyActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println(“Button clicked and the number is + “ fourtyTwo); } }); Now the question that stands is how to achieve the same functionality using lambdas? The following snippet doesn't compile: myButton.addActionListener(e -> System.out.println(“Button clicked and the number is” + fourtyTwo); because the lambda expression we're using is actually the implementation of the ActionListener#actionPerformed(ActionEvent e) method, not the M yActionListener#actionPerformed(ActionEvent e) method and this is why it fails to compile - the compiler doesn't know about the variable fourtyTwo since it's not defined in the ActionListener interface. Now the problem is how to tell the JVM that the lambda we're using is actual implementaion of the M yActionListener#actionPerformed(ActionEvent e)? And the answer is the following: In Java 8 the abstract classes are not functional interfaces, even if they contain a single abstract method! This is why, we can't cast the lambda to M yActionListener when passing and to expect to have https://weblogs.java.net/blog/kocko/archive/2014/06/19/how-tell-java-apply-lambda-expressions-custom-functional-interface-instead-one-methods-contract 1/2 6/24/2014 How to tell Java to use a custom functional interface ... | Java.net our hands on the variable named fourtyTwo. On the other hand, however, functional interfaces can be extended and so we can create functional interfaces of our own. public interface MyActionListener extends ActionListener { int fourtyTwo = 42; } This is enough to say that our interfaces is functional, because M yActionListener is an interface and not an abstract class M yActionListener has a single abstract method and this is the derived ActionListener#actionPerformed(ActionEvent e) Now we can write the lambda expression, which would be an implementation of M yActionListener#actionPerformed(ActionEvent e) and pass it to the myButton.addActionListener() method: MyActionListener myActionListenerLambda = e -> System.out.println(“Button is clicked and the number is “ + fourtyTwo myButton.addActionListener(myActionListenerLambda); it compiles and successfully accesses the member variable and therefore the problem is solved. Cheers! Related Topics >> J2SE Blog Links >> Login or register to post comments blog 276 reads Feedback FAQ Terms of Use Printer-friendly version Privacy kocko's Trademarks Your use of this web site or any of its content or softwar e indicates your agr eement to be bound by these Ter ms of Par ticipation. Copyr ight © 2014, Or acle and/or its affiliates. All r ights r eser ved. Or acle and Java ar e r egister ed tr ademar ks of Or acle and/or its affiliates. Other names may be tr ademar ks of their r espective owner s. Power ed by Or acle, Pr oject Kenai and Cognisync https://weblogs.java.net/blog/kocko/archive/2014/06/19/how-tell-java-apply-lambda-expressions-custom-functional-interface-instead-one-methods-contract 2/2