Java 8 Lambda Expressions – what about multiple methods in nested class

From JLS 9.8

A functional interface is an interface that has just one abstract method, and thus
represents a single function contract.

Lambdas require these functional interfaces so are restricted to their single method. Anonymous interfaces still need to be used for implementing multi-method interfaces.

addMouseListener(new MouseAdapter() {

    @Override
    public void mouseReleased(MouseEvent e) {
       ...
    }

    @Override
    public void mousePressed(MouseEvent e) {
      ...
    }
});

Leave a Comment