Sub topic 2
Functional Interfaces: One Abstract Method
In the last chapter you passed behavior to sort by wrapping one method inside an anonymous class. That worked because Comparator is a special kind of interface. This chapter explains what makes it special.
What Is a Functional Interface?
An abstract method is a method with a name and parameters but no body. A class that implements the interface must supply the body.
A functional interface is an interface with exactly one abstract method. People also call it a SAM type, short for "single abstract method."
A lambda in Java is always written as the body of that one method. This is why a lambda needs a functional interface. The interface tells Java the method's name, its parameter types, and its return type.
Examples You Already Know
Java has many functional interfaces built in. Here are three common ones.
| Interface | Its one abstract method | What it does |
|---|---|---|
| `Runnable` | `void run()` | Runs some code |
| `Comparator<T>` | `int compare(T a, T b)` | Orders two values |
| `Callable<V>` | `V call()` | Computes a result |
A Worked Example
Here is a small functional interface called Greeter. Below it, two classes implement it in the old way, without lambdas.
interface Greeter {
String greet(String name); // the one abstract method
}
class PoliteGreeter implements Greeter {
@Override
public String greet(String name) {
return "Good day, " + name;
}
}
public class GreeterDemo {
public static void main(String[] args) {
Greeter polite = new PoliteGreeter();
Greeter casual = new Greeter() {
@Override
public String greet(String name) {
return "Hey, " + name;
}
};
System.out.println(polite.greet("Ada")); // Good day, Ada
System.out.println(casual.greet("Ada")); // Hey, Ada
}
}Greeter has only one abstract method, so it is a functional interface. In the next chapter you will replace the anonymous class with one short lambda.
What Does Not Count
An interface can hold other methods and still be functional. Only abstract methods count toward the limit of one.
- A default method is an interface method that already has a body. It does not count.
- A static method belongs to the interface itself. It does not count either.
- Methods that every object gets from
Object, such asequals, do not count. This is whyComparatordeclaresequalsand is still functional.
interface Shape {
double area(); // the only abstract method
default String describe() { // has a body, so it does not count
return "Area: " + area();
}
static Shape unitSquare() { // static, so it does not count
return new Shape() {
@Override
public double area() {
return 1.0;
}
};
}
}
public class ShapeDemo {
public static void main(String[] args) {
System.out.println(Shape.unitSquare().describe()); // Area: 1.0
}
}Exercise
Is this interface functional? interface Printer { void print(String s); default void printTwice(String s) { print(s); print(s); } boolean equals(Object o); }
Show answer
Yes. The only abstract method that counts is print. printTwice is a default method with a body, and equals comes from Object, so neither one counts.
Exercise
Why does a lambda need a functional interface?
Show answer
A lambda has no name of its own. The functional interface gives it one method to stand in for, and that method supplies the name, the parameter types, and the return type.