Functional interfaces & @FunctionalInterface
One abstract method — the foundation for lambda expressions in Module 11.
What makes an interface functional
A functional interface has exactly one abstract method. Default, static, and private methods do not count toward this limit, nor do methods inherited from Object (like equals):
@FunctionalInterface
public interface Transformer {
String transform(String input); // the single abstract method
// default and static methods are OK here
}
The @FunctionalInterface annotation asks the compiler to verify that exactly one abstract method exists. If you accidentally add a second, you get a compile error rather than a silent lambda breakage.
Why this matters: Java 8 lambdas are shorthand for an anonymous class that implements a functional interface:
Transformer upper = s -> s.toUpperCase(); // lambda — Module 11
Transformer upper = new Transformer() { // verbose equivalent
@Override
public String transform(String input) {
return input.toUpperCase();
}
};
The standard library's java.util.function package ships many ready-made functional interfaces:
| Interface | Abstract method | Purpose |
|---|---|---|
| Predicate<T> | boolean test(T t) | filter / check |
| Function<T,R> | R apply(T t) | transform |
| Consumer<T> | void accept(T t) | side effect |
| Supplier<T> | T get() | produce a value |
You will use these heavily in the lambdas and streams modules.