Default and Static Methods Don't Count
You'll learn: How @FunctionalInterface allows you to add as many default and static methods as you want without breaking the single abstract method rule.
The Single Method Rule Stays Intact
When you mark an interface with @FunctionalInterface, the compiler enforces that it has exactly one abstract method—but here's the important part: default and static methods don't count toward that limit. You can add dozens of them, and the interface remains functional.
Why This Makes Sense
Think of a functional interface like a contract for one specific action. That action is the abstract method. Default and static methods are like bonus utilities that come with the contract—they provide helper functionality or common implementations, but they don't change the core "one action" nature of the interface.
The abstract method is what the lambda or method reference will implement. Default methods already have implementations, so they don't need to be provided by the lambda. Static methods belong to the interface itself, not to instances.
Example in Plain Terms
Imagine you have a functional interface called Processor with one abstract method process(). You can add:
- A default method
preProcess()that provides common setup logic - Another default method
postProcess()for cleanup - A static method
createDefault()that returns a standard implementation - A static method
combine()that merges two processors
None of these additions violate the @FunctionalInterface contract because you still have exactly one abstract method that defines what the lambda must do.
Practical Impact
This flexibility means functional interfaces can be rich with utility methods while staying lambda-friendly. The java.util.function interfaces use this heavily—for example, Function has default methods like andThen() and compose() alongside its single abstract apply() method.
Key Takeaway: @FunctionalInterface only counts abstract methods—default and static methods can exist in unlimited numbers without affecting the functional nature of the interface.