What is a Functional Interface in OOP?
Learn what a functional interface is in OOP — the single abstract method rule, lambdas, and java.util.function — with a Java example.
Expected Interview Answer
A functional interface is an interface that declares exactly one abstract method, which makes it a valid target type for a lambda expression or a method reference, even though it may also contain default and static methods.
The single-abstract-method (SAM) rule is what allows the compiler to infer that a lambda’s body implements that one method — the lambda itself is essentially an anonymous, inline implementation of the interface’s sole abstract method. Java’s java.util.function package ships a standard library of them: Function<T,R> (one input, one output), Predicate<T> (one input, boolean output), Supplier<T> (no input, one output), and Consumer<T> (one input, no output), among others. The optional @FunctionalInterface annotation does not change behavior but instructs the compiler to fail the build if a second abstract method is accidentally added, guarding the contract. Default and static methods don’t count toward the one-abstract-method limit, so a functional interface can still offer utility methods alongside its single abstract one.
- Enables lambda expressions and method references as concise implementations
- Provides a rich standard vocabulary via java.util.function
- Supports functional-style, declarative code within an OOP language
- @FunctionalInterface guards the single-method contract at compile time
AI Mentor Explanation
A "SelectionCriterion" contract that only ever asks one question — evaluate(player) returns true or false — lets a selector hand over a short rule like "average above 40" as an inline decision instead of writing a whole named class for it. Because the contract has exactly one method to fill in, the compiler can treat that inline rule as a complete implementation. That is a functional interface: an interface with a single abstract method, letting a lambda stand in directly for a full implementing class.
Step-by-Step Explanation
Step 1
Declare exactly one abstract method
The interface may include default/static methods but only one method has no body.
Step 2
Optionally annotate @FunctionalInterface
This documents intent and makes the compiler enforce the single-abstract-method rule.
Step 3
Assign a lambda or method reference
The compiler treats the lambda body as the implementation of the sole abstract method.
Step 4
Use it as the interface type
The lambda can be passed anywhere the functional interface type is expected.
What Interviewer Expects
- Correct SAM (single abstract method) definition
- Knowledge that default/static methods don't count toward the limit
- Familiarity with java.util.function types: Function, Predicate, Supplier, Consumer
- Understanding of what @FunctionalInterface actually enforces (compile-time check, not new behavior)
Common Mistakes
- Believing @FunctionalInterface is required for lambdas to work (it is optional documentation/enforcement)
- Thinking default methods count toward the single-abstract-method limit
- Confusing a functional interface with a marker interface
- Not knowing common java.util.function types like Function, Predicate, Supplier, Consumer
Best Answer (HR Friendly)
“A functional interface is an interface that has exactly one abstract method, which is what allows you to implement it with a short lambda expression instead of writing a whole class. Java’s built-in examples include Function, Predicate, and Consumer, and you can mark your own with @FunctionalInterface so the compiler enforces that it never accidentally grows a second abstract method.”
Code Example
@FunctionalInterface
interface Calculator {
int apply(int a, int b); // the single abstract method
}
Calculator add = (a, b) -> a + b; // lambda implements apply()
Calculator multiply = (a, b) -> a * b;
System.out.println(add.apply(2, 3)); // 5
System.out.println(multiply.apply(2, 3)); // 6Follow-up Questions
- What does the @FunctionalInterface annotation actually enforce?
- Name a few functional interfaces from java.util.function and their signatures.
- Can a functional interface have default or static methods?
- How does a method reference relate to a functional interface?
MCQ Practice
1. A functional interface is defined as an interface with?
The single-abstract-method (SAM) rule is what makes an interface a valid target for a lambda expression.
2. Which of these counts toward the “one abstract method” limit of a functional interface?
Only unimplemented (abstract) methods count; default and static methods have bodies and do not count.
3. Which java.util.function interface represents a one-argument function returning a boolean?
Predicate<T> takes one argument and returns a boolean, commonly used for filtering.
Flash Cards
Functional interface in one line? — An interface with exactly one abstract method, valid as a lambda target type.
What does @FunctionalInterface do? — Documents intent and makes the compiler enforce the single-abstract-method rule.
Do default methods count toward the limit? — No — only unimplemented abstract methods count.
Name two built-in functional interfaces? — Function<T,R> and Predicate<T> from java.util.function.