Java Functional Interfaces: The Single Abstract Method Rule (OCP 21)
Functional interfaces for the OCP 21 exam: the single abstract method rule, what @FunctionalInterface checks, inherited and merged abstract methods, why default, static and private methods don't count, the Object-method exception, and what can't be a lambda target.
kvi5hnuCode Sprout team member6 min readOn this page
The previous article showed that a lambda always implements an interface. Not every interface qualifies, though. A lambda supplies the body of one method, so it can only implement an interface that has exactly one abstract method. Such an interface is a functional interface, and the requirement is often called the SAM rule (single abstract method).
@FunctionalInterface
interface Discount {
double apply(double price);
}
Discount minus5 = p -> p - 5;
System.out.println(minus5.apply(80)); // 75.0
A class can still implement Discount the normal way. Being functional only adds the option of using a lambda or method reference.
The @FunctionalInterface annotation
@FunctionalInterface asks the compiler to check that the interface follows the SAM rule. If it doesn't, compilation fails:
@FunctionalInterface
interface Workout { // DOES NOT COMPILE: multiple non-overriding abstract methods found
void warmUp();
void train();
}
@FunctionalInterface
interface Idle { // DOES NOT COMPILE: no abstract method found
default void rest() { }
}
It's only allowed on interfaces. Putting it on a class, enum or record is also an error.
The annotation is optional. What makes an interface functional is its shape, not the annotation. An interface with one abstract method can be used with lambdas whether or not it's annotated. The annotation documents the intent and protects it: if someone later adds a second abstract method, the interface itself fails to compile, instead of every lambda that uses it.
Counting abstract methods
To decide whether an interface is functional, count its abstract methods, including inherited ones. Several kinds of members don't count:

Methods with bodies don't count. default, static and private (including private static) methods are already implemented:
@FunctionalInterface
interface Discount {
double apply(double price); // the one abstract method
default Discount then(Discount next) { return p -> next.apply(apply(p)); }
static Discount percent(int pct) { check(pct); return p -> p * (100 - pct) / 100.0; }
private static void check(int pct) { if (pct < 0 || pct > 100) throw new IllegalArgumentException(); }
}
Discount has four methods but only one abstract method, so it's still functional. A default method can be overridden, but it doesn't need to be, so it doesn't count.
Inherited abstract methods count.
interface MemberDiscount extends Discount { } // functional: inherits apply()
interface Coupon extends Discount { String code(); } // NOT functional: apply() + code()
MemberDiscount declares nothing, but it inherits exactly one abstract method, so MemberDiscount m = p -> p / 2; compiles. Coupon has two abstract methods, so trying to assign it a lambda gives "Coupon is not a functional interface".
Matching inherited methods merge. If an interface inherits the same abstract method (same signature and compatible return type) from two parents, it counts once:
interface Starter { void begin(); }
interface Launcher { void begin(); }
@FunctionalInterface
interface Engine extends Starter, Launcher { } // compiles: one abstract method, begin()
A default can remove an abstract method, and an abstract redeclaration can add one back. If a sub-interface provides a default body for its parent's only abstract method, it has no abstract methods and isn't functional. In the other direction, a sub-interface can redeclare an inherited default method as abstract, and that redeclared method counts.
The Object method exception
Every class extends Object. So if an interface redeclares one of Object's public methods as abstract, every implementing class already has it, and it doesn't count toward the SAM rule. The three methods to know are:
public String toString()public boolean equals(Object obj)public int hashCode()
@FunctionalInterface
interface Label {
String text(double price); // the one that counts
String toString(); // ignored
boolean equals(Object other); // ignored
int hashCode(); // ignored
}
Label is functional. An interface whose only abstract method is String toString(); isn't, because after the exception it has zero abstract methods.
Watch for methods that only look like Object methods:
| Declaration in the interface | Counts? | Why |
|---|---|---|
boolean equals(Object o); | no | matches Object.equals(Object) |
boolean equals(Label o); | yes | different parameter type, so it's an overload, not Object's method |
String toString(); | no | matches Object.toString() |
int toString(); | compile error | it conflicts with Object.toString(), which returns String |
Object clone(); | yes | clone() is protected in Object, not public |
So interface Rest { boolean equals(Rest r); void nap(); } has two abstract methods and isn't functional.
What can't be a lambda target
The SAM rule applies only to interfaces. A lambda can't implement:
- an abstract class, even one with a single abstract method ("Abs is not a functional interface")
- a sealed interface, because a lambda's class could never appear in a
permitslist - an interface whose single abstract method is generic, such as
<T> T make();("invalid functional descriptor for lambda expression"). A method reference can still implement it.
For these, use a named class or an anonymous class.
What's current
- Nothing in this article changed in Java 22 through 25, so the rules apply to both the Java 21 exam (1Z0-830) and the Java 25 exam (1Z0-831).
@FunctionalInterfaceand the single-abstract-method rule date from Java 8. No new functional interfaces were added tojava.util.functionafter Java 8.
Exam traps from this article
A complete example
Discount is functional despite having default, static and private methods. Label redeclares Object methods, MemberDiscount inherits its method, and Coupon has two abstract methods, so it needs an anonymous class. Reflection then lists each interface's abstract methods.
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class Solution {
// One abstract method plus default, static and private helpers: still functional
@FunctionalInterface
interface Discount {
double apply(double price);
default Discount then(Discount next) {
return p -> next.apply(apply(p));
}
static Discount percent(int pct) {
check(pct);
return p -> p * (100 - pct) / 100.0;
}
private static void check(int pct) {
if (pct < 0 || pct > 100) throw new IllegalArgumentException("bad percent " + pct);
}
}
// Redeclared Object methods don't count: still exactly one abstract method
@FunctionalInterface
interface Label {
String text(double price);
String toString();
boolean equals(Object other);
int hashCode();
}
// Inherits its single abstract method and adds none
interface MemberDiscount extends Discount { }
// Two abstract methods: an ordinary interface, not functional
interface Coupon {
double apply(double price);
String code();
}
static List<String> abstractMethods(Class<?> type) {
List<String> names = new ArrayList<>();
for (Method m : type.getMethods()) {
if (Modifier.isAbstract(m.getModifiers())) names.add(m.getName());
}
names.sort(null);
return names;
}
public static void main(String[] args) {
Discount minus5 = p -> p - 5;
Discount tenOff = Discount.percent(10);
Discount both = minus5.then(tenOff);
System.out.println("80 -> " + both.apply(80));
MemberDiscount member = p -> p / 2;
System.out.println("member 80 -> " + member.apply(80));
Label label = p -> String.format(Locale.ROOT, "$%.2f", p);
System.out.println("label: " + label.text(both.apply(80)));
// A Coupon can't be a lambda, so it takes an anonymous class
Coupon spring = new Coupon() {
public double apply(double price) { return price - 12; }
public String code() { return "SPRING12"; }
};
System.out.println(spring.code() + " on 80 -> " + spring.apply(80));
System.out.println("abstract methods in Discount: " + abstractMethods(Discount.class));
System.out.println("abstract methods in Label: " + abstractMethods(Label.class));
System.out.println("abstract methods in Coupon: " + abstractMethods(Coupon.class));
try {
Discount.percent(120);
} catch (IllegalArgumentException e) {
System.out.println("rejected: " + e.getMessage());
}
}
}
Output:
80 -> 67.5
member 80 -> 40.0
label: $67.50
SPRING12 on 80 -> 68.0
abstract methods in Discount: [apply]
abstract methods in Label: [equals, hashCode, text, toString]
abstract methods in Coupon: [apply, code]
rejected: bad percent 120
Points to notice:
bothis built by the default methodthen, which itself returns a lambda. Subtracting 5 first and then taking 10% off gives75 * 0.9 = 67.5.MemberDiscountadds no methods, so a lambda implements the inheritedapply.- Reflection reports four abstract methods for
Label, but the compiler ignores the threeObjectones, soLabelis still a valid lambda target. Couponhas two abstract methods, so it needs an anonymous class.- The
private statichelpercheckvalidates input for thestaticfactory method.
Common questions
Why should I annotate my functional interfaces?
It turns a design intention into a compiler check. Many JDK interfaces, such as Runnable, Comparator and everything in java.util.function, carry it.
Is Comparator really functional? It has equals and many other methods.
Yes. It has one abstract method, compare. Its equals(Object) is an Object method, and the rest are default or static.
Can a functional interface be generic?
Yes, the interface can: interface Mapper<T, R> { R map(T t); } is fine. Only a generic method (<T> T make();) blocks lambdas.
Summary
- A functional interface has exactly one abstract method, and only such an interface can be the target of a lambda or method reference.
@FunctionalInterfaceis optional. It makes the compiler reject interfaces with zero or several abstract methods.- Inherited abstract methods count, identical inherited methods merge, and default, static and private methods don't count.
- Abstract redeclarations of
Object's publictoString,equals(Object)andhashCodeare ignored. Look-alikes such asequals(OtherType)andclone()are counted. - Abstract classes, sealed interfaces and interfaces with a generic abstract method can't be implemented by lambdas.
Practice quiz
Test yourself with the functional interfaces quiz: 10 exam-style questions with explanations.
Previous: Java Lambda Expressions: Syntax, Parameters and Bodies (OCP 21)
Next: Java Method References: Static, Bound, Unbound and Constructor (OCP 21)
Related reading
- Java Lambda Expressions: Syntax, Parameters and Bodies (OCP 21)
- Symmetric Encryption in Java: AES-GCM, Key Generation and Cipher Streams
- Streaming XML Parsers in Java: SAX and StAX
- Java Directory Traversal: Files.list, walk, find, Glob Patterns and FileVisitor
- Java ZIP Archives: ZipInputStream, ZipOutputStream and ZIP File Systems

More in OCP Java 21
OCP Java 21 Mock Exam 3 (1Z0-830)
The third full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830), weighted toward the hardest areas: streams, concurrency, generics, modules and I/O. 120 minutes, 68% to pass, with explanations.
OCP Java 21 Mock Exam 2 (1Z0-830)
The second full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830): new questions on every objective, with more multi-select. 120 minutes, 68% to pass, with an explanation for every answer.
OCP Java 21 Mock Exam 1 (1Z0-830)
A full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830), covering every objective at exam difficulty. 120 minutes, 68% to pass, with an explanation for every answer.
Quiz: Final Review (OCP 21)
Ten exam-style questions mixing the most-tested OCP Java 21 rules from the final checklist: promotion, String immutability, overloading, initialization order, collections, streams, exceptions and switch patterns, with explanations.
Discussion
Sign in to ask a question or share what you learned.