What are Functional Interfaces in Java?
Learn what a functional interface in Java is, the single abstract method rule, @FunctionalInterface, built-in types like Predicate and Function, and examples.
Expected Interview Answer
A functional interface in Java is an interface that declares exactly one abstract method, which lets it serve as the target type for a lambda expression or method reference.
Because it has a single abstract method (SAM), the compiler can infer that a lambda's body implements that one method. It may still contain default and static methods, plus any number of methods inherited from Object, without breaking the rule. The optional @FunctionalInterface annotation asks the compiler to enforce the single-abstract-method constraint. Java provides built-in functional interfaces in java.util.function such as Predicate, Function, Consumer, and Supplier.
- Enables lambda expressions and method references
- Reduces boilerplate anonymous classes
- Encourages a functional, declarative style
- @FunctionalInterface catches accidental extra methods
- Rich ready-made set in java.util.function
AI Mentor Explanation
A functional interface is like a single-role specialist in a cricket squad: a designated 12th-man fielder with exactly one job. Because the role is unambiguous, the captain can hand it to anyone quickly — a lambda — without spelling out a full contract. One method, one clear task, so a short instruction is enough to fill the position.
Step-by-Step Explanation
Step 1
Declare one abstract method
Define an interface with a single abstract method (the SAM), which makes it functional.
Step 2
Add @FunctionalInterface
Optionally annotate it so the compiler enforces exactly one abstract method.
Step 3
Include defaults if needed
Add default or static methods freely — they do not count against the single-method rule.
Step 4
Implement with a lambda
Provide the behavior with a lambda expression or a method reference instead of an anonymous class.
Step 5
Reuse built-ins
Prefer java.util.function types like Predicate, Function, Consumer, and Supplier when they fit.
What Interviewer Expects
- Definition: interface with exactly one abstract method
- Role of the @FunctionalInterface annotation
- Relationship to lambdas and method references
- Awareness that default/static/Object methods are allowed
- Knowledge of built-in functional interfaces
Common Mistakes
- Thinking a functional interface can have no methods at all
- Believing default methods break the single-abstract-method rule
- Assuming @FunctionalInterface is mandatory for lambdas
- Confusing Predicate, Function and Consumer signatures
- Adding a second abstract method and expecting lambdas to still work
Best Answer (HR Friendly)
“A functional interface is an interface with just one required method, which lets developers plug in short lambda expressions instead of writing bulky classes. Java ships with many ready-made ones, like Predicate and Function, making code cleaner and more expressive.”
Code Example
import java.util.function.Predicate;
@FunctionalInterface
interface Calculator {
int operate(int a, int b); // single abstract method
default void log() { // default method allowed
System.out.println("Calculating...");
}
}
public class FunctionalDemo {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b; // lambda implements the SAM
System.out.println(add.operate(3, 4)); // 7
Predicate<Integer> isEven = n -> n % 2 == 0; // built-in
System.out.println(isEven.test(10)); // true
}
}Follow-up Questions
- Name the four core functional interfaces in java.util.function.
- Can a functional interface have default and static methods?
- Is the @FunctionalInterface annotation required to use lambdas?
- What is the difference between Function and BiFunction?
- How does a method reference relate to a functional interface?
MCQ Practice
1. How many abstract methods does a functional interface have?
A functional interface has exactly one abstract method (the SAM), which lets a lambda target it.
2. Which built-in functional interface takes one argument and returns a boolean?
Predicate<T> defines boolean test(T t), taking one argument and returning a boolean.
3. Does adding a default method break the functional interface rule?
Default and static methods do not count as abstract methods, so they do not violate the single-abstract-method rule.
Flash Cards
What defines a functional interface? — An interface with exactly one abstract method (SAM), usable as a lambda target.
What does @FunctionalInterface do? — It asks the compiler to enforce that the interface has exactly one abstract method.
Name four core java.util.function interfaces. — Predicate, Function, Consumer, and Supplier.
Are default methods counted against the SAM rule? — No. Default, static, and Object methods do not count as the abstract method.