☕ Java Intermediate

What is a functional interface in Java?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

Answer

A functional interface is an interface that has exactly one abstract method (SAM — Single Abstract Method). They are the target types for lambda expressions and method references. The @FunctionalInterface annotation is optional but recommended — it causes a compile error if the interface has more than one abstract method. Java 8 built-in functional interfaces in java.util.function: Predicate<T> (takes T, returns boolean), Function<T,R> (takes T, returns R), Consumer<T> (takes T, returns void), Supplier<T> (takes nothing, returns T), BiFunction<T,U,R>, UnaryOperator<T>. Existing interfaces like Runnable and Comparator are also functional interfaces.

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Java answers easy to follow.