Search code examples
javafunctional-interface

if functional interface extends another interface is it still a functional interface?


Java 8 in Action book by Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft stats that:

public interface Adder{
    int add(int a, int b);
}
public interface SmartAdder extends Adder{
    int add(double a, double b);
}

SmartAdder isn’t a functional interface because it specifies two abstract methods called add (one is inherited from Adder).

In an other similar example in the book the following interface called as functional interface.

public interface ActionListener extends EventListener {
     void actionPerformed(ActionEvent e);
}

What makes the first example not functional interface compare to the second example?


Solution

  • Because SmartAdder provides two method definitions (i.e. add is overloaded, not overridden):

    • int add(double a, double b);, and
    • int add(int a, int b); (from parent)

    Conversely, EventListener is a marker interface, so ActionListener only provides one method definition, its own actionPerformed.