- Home
- Interview Questions
- Java
- What makes an interface a functional interface, and what doe…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: A functional interface has exactly one abstract method, so a lambda or method reference can implement it. The @FunctionalInterface annotation is optional: it makes the compiler reject the interface if it does not have exactly one abstract method, and it documents your intent.
Detailed explanation
A lambda has no name and no declared type of its own. The compiler works out what it means from the target type, which is the interface it is assigned to. To map the lambda body to a method, that interface must have a single abstract method (SAM). Interfaces like this are called functional interfaces.
What counts towards the "exactly one abstract method" rule:
- Default methods do not count. They already have a body.
java.util.function.FunctionhasandThenandcomposeas defaults and is still functional. - Static methods do not count.
Comparator.comparingis static. - Abstract methods that match public methods of
java.lang.Objectdo not count.Comparatordeclaresboolean equals(Object obj), but every implementing class already inheritsequalsfromObject. Socompareis still its only real abstract method. - Inherited abstract methods do count. An interface that extends a functional interface and adds no new abstract method is still functional. If it adds one more, it is not.
What @FunctionalInterface does:
- It is a compile-time check only. If the interface has zero or more than one abstract method, compilation fails.
- It has no runtime effect. Without it, any SAM interface still works with lambdas.
Runnable,CallableandComparatorexisted before Java 8 and became lambda-friendly with no change to their single-method shape. - It protects your API. If a teammate later adds a second abstract method, the build fails at the interface itself. Without it, the build breaks at every lambda that uses the interface.
Before writing your own interface, check java.util.function. It has Function<T,R>, Predicate<T>, Consumer<T>, Supplier<T>, UnaryOperator<T>, BinaryOperator<T>, the Bi versions, and primitive versions such as IntPredicate that avoid boxing.
Example
@FunctionalInterface
interface DiscountRule {
double apply(double price); // the single abstract method
default DiscountRule then(DiscountRule next) { // allowed
return p -> next.apply(apply(p));
}
static DiscountRule none() { // allowed
return p -> p;
}
String toString(); // Object method, not counted
// double cap(double p); // uncommenting this breaks compilation
}
public class Shop {
public static void main(String[] args) {
DiscountRule tenPercent = p -> p * 0.90;
DiscountRule flat50 = p -> Math.max(0, p - 50);
DiscountRule combined = tenPercent.then(flat50);
System.out.println(combined.apply(1000)); // 850.0
}
}The interface has default, static and Object-matching methods, but only apply is abstract. So lambdas can target it, and the annotation compiles without error.
Common mistakes
- Saying a functional interface can have "only one method". It can have many default and static methods. Only the abstract count matters.
- Believing
@FunctionalInterfaceis required for lambdas to work. It is only a compiler check and a piece of documentation. - Forgetting that redeclared
Objectmethods likeequalsare excluded. This is whyComparatorlooks like it has two abstract methods but is functional. - Writing custom interfaces such as
StringCheckerwhenPredicate<String>already does the job. This makes code harder to compose with the standard library.
Follow-up questions
- Why can a lambda not implement a functional interface whose abstract method is itself generic, while a method reference can?
- If an interface extends two functional interfaces with identical abstract method signatures, is it still functional?
- When would you choose
IntFunctionorToIntFunctionoverFunction<Integer,Integer>?
Interview tip: Name Comparator as the example of the Object-method exception. It shows you know the rule precisely, not just the textbook definition.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More easy Java questions
Related Questions
- How is a lambda different from an anonymous inner class in Java beyond shorter syntax? · Java · 1 ans
- How does HashMap work internally, including collisions, resizing and treeification in Java 8? · Java · 1 ans
- What is the difference between HashSet, LinkedHashSet and TreeSet, and when would you use each? · Java · 1 ans
- Why do lambdas created inside a loop all return the last loop value in Python? · Python · 1 ans
- How do you implement a generic interface and its functions in Java? · Java · 1 ans
- What are the constraints in Java generics and why are they used? · Java · 1 ans
- How do abstract classes differ from interfaces in Java? · Java · 1 ans
- What are the advantages of using interfaces in Java? · Java · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)