Java · OOP, JVM and backend craft
What is a functional interface and which ones does Java provide?
Short answer
A functional interface has exactly one abstract method, which makes it a target for lambdas and method references. Java provides standard shapes such as Function, Supplier, Consumer and Predicate.
The single abstract method is often called SAM. Default and static methods do not count, so an interface can have many helper methods and still be functional. @FunctionalInterface is optional but makes the compiler reject accidental extra abstract methods.
@FunctionalInterface
interface Validator<T> {
boolean test(T value);
default Validator<T> negate() {
return value -> !test(value);
}
}
Validator<String> notEmpty = value -> !value.isEmpty();The standard shapes
- Function<T, R> transforms a value; BiFunction takes two inputs
- Supplier<T> produces a value with no input
- Consumer<T> accepts a value and returns nothing
- Predicate<T> returns a boolean and supports and, or and negate
- UnaryOperator and BinaryOperator specialise Function for same-type results
- Primitive variants such as IntPredicate avoid boxing
Choose the most specific standard interface before creating a custom one. A custom interface is worthwhile when the name conveys domain meaning, such as PriceRule, or when the signature does not match a standard shape.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Does a default method break the functional interface contract?
- 2When would you define a custom functional interface?
- 3Why do primitive functional interfaces exist?
- 4What does @FunctionalInterface enforce?
Say it in the interview
A functional interface has one abstract method, so a lambda or method reference can implement it. Default and static methods are allowed because they are not abstract. Java gives me Function, Supplier, Consumer, Predicate and primitive variants, which cover most cases. I only define a custom one when the domain name adds clarity or the signature does not fit a standard interface.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Java mock interview and get scored feedback on depth, clarity and confidence.