Error Messages When Violating the Contract
What you'll learn: You'll discover exactly what compiler errors appear when you try to add a second abstract method to an interface marked with @FunctionalInterface.
Understanding the Violation
When you annotate an interface with @FunctionalInterface, you're making a promise to the compiler: "This interface has exactly one abstract method." If you break that promise by adding a second abstract method, the compiler immediately catches the violation and refuses to compile your code.
This is one of the primary benefits of using the annotation—it provides compile-time safety rather than letting the error surface later when someone tries to use your interface with a lambda expression.
The Actual Error Message
Here's what happens when you violate the contract:
@FunctionalInterface
public interface Processor {
void process(String data);
int count(String data); // Second abstract method - violation!
}
Compiler error:
error: Unexpected @FunctionalInterface annotation
Processor is not a functional interface
multiple non-overriding abstract methods found in interface Processor
The error message is explicit and helpful. It tells you:
- The annotation doesn't match reality
- The interface is not functional
- Multiple abstract methods were found
Why This Matters
Think of @FunctionalInterface like a contract clause with a penalty for breach. Without the annotation, the code would compile fine—you'd just have a regular interface. But when you use the annotation, you're asking the compiler to be your safety net, catching mistakes before they cause lambda expression failures downstream.
Key Takeaway: The @FunctionalInterface annotation triggers immediate, clear compiler errors when you accidentally add multiple abstract methods, preventing the interface from being used with lambdas and catching design mistakes early.