Skip to main content

How to tell Java to apply a lambda expressions for a custom functional interface instead for the one from the method's contract?

Posted by kocko 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 MyActionListener and even to refer the nester member within the implementation:

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 MyActionListener#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 MyActionListener#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 MyActionListener when passing and to expect to have 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.

@FunctionalInterface
public interface MyActionListener extends ActionListener {
    int fourtyTwo = 42;
}

This is enough to say that our interface is functional, because

  • MyActionListener is an interface and not an abstract class
  • MyActionListener 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 MyActionListener#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 >>