Default and Static Methods Are Allowed
You'll learn: How functional interfaces can include multiple default and static methods without breaking the Single Abstract Method (SAM) rule.
The Core Idea
A functional interface must have exactly one abstract method—that's the SAM rule you learned. But here's the twist: you can add as many default and static methods as you want without violating this rule. Why? Because default and static methods already have implementations; they're not abstract.
Think of it like a job description: a functional interface says "you must implement this one task" (the abstract method), but it also provides helpful tools and utilities (default and static methods) that are already built and ready to use.
Why This Matters
This flexibility lets functional interface designers provide useful helper methods alongside the core abstract method. The single abstract method remains the "functional" part—the one behavior users must define—while default and static methods offer convenience.
@FunctionalInterface
interface Calculator {
// The single abstract method
int calculate(int a, int b);
// Default method - provides implementation
default int square(int n) {
return calculate(n, n);
}
// Static method - utility function
static int add(int a, int b) {
return a + b;
}
}
This is still a valid functional interface because only calculate is abstract. The default square method and static add method don't count toward the SAM limit.
Calculator multiply = (a, b) -> a * b;
int result = multiply.calculate(5, 3); // 15
int squared = multiply.square(4); // 16 (uses calculate)
int sum = Calculator.add(10, 20); // 30 (static, called on interface)
Key Takeaway: Functional interfaces allow unlimited default and static methods because only abstract methods count toward the "single abstract method" rule—default and static methods are already implemented.