Single Abstract Method (SAM) Rule
You'll learn: Why functional interfaces must have exactly one abstract method to work as lambda targets.
The Core Principle
The Single Abstract Method (SAM) Rule is the defining characteristic of a functional interface. A functional interface may have multiple default or static methods, but it must contain precisely one abstract method. This single abstract method becomes the target for lambda expressions and method references.
Think of it like a contract with one blank line to fill: the interface says "I need you to implement this one thing," and a lambda provides that implementation. If there were two blank lines (two abstract methods), the compiler wouldn't know which one your lambda is meant to fulfill.
Why Exactly One?
Lambda expressions provide a compact syntax for implementing methods. When you write (x) -> x * 2, Java needs to know unambiguously which method you're implementing. With exactly one abstract method, there's no confusion—the lambda matches that method's signature.
// Valid functional interface - one abstract method
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
// These don't count toward the SAM limit
default int add(int a, int b) { return a + b; }
static int multiply(int a, int b) { return a * b; }
}
// Lambda knows it implements calculate()
Calculator sub = (a, b) -> a - b;
If Calculator had two abstract methods, the compiler would reject the lambda—it wouldn't know whether you meant to implement the first or second method.
The @FunctionalInterface Annotation
While optional, @FunctionalInterface tells the compiler to verify the SAM rule. If you accidentally add a second abstract method, you'll get a compile error immediately.
Key Takeaway: The SAM Rule ensures functional interfaces have exactly one abstract method, making lambda expressions unambiguous—the compiler always knows which method the lambda implements.