Quick revision
The short answer
An interface is functional when it has exactly one abstract method, so a lambda or method reference can supply that method's body. Default and static methods do not count, and neither do abstract methods that match public methods of Object, which is why Comparator, which declares compare and equals, is still functional. @FunctionalInterface is optional: it makes the compiler check the rule and documents the intent. java.util.function provides the standard ones, such as Function, Supplier, Consumer, Predicate, BiFunction, UnaryOperator and their primitive variants.
What to remember
In this answer, we saw that a functional interface has exactly one abstract method, which is what a lambda or method reference implements. Default and static methods are not counted, and neither are abstract methods matching public Object methods, which is why Comparator, with compare and equals, is still functional. @FunctionalInterface is optional, but it makes the compiler enforce the rule and documents intent. And java.util.function supplies Function, Supplier, Consumer, Predicate, the two-argument and operator variants, and primitive specialisations that avoid boxing.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
An interface is functional when it has exactly one abstract method, so a lambda or method reference can supply that method's body. Default and static methods do not count, and neither do abstract methods that match public methods of Object, which is why Comparator, which declares compare and equals, is still functional. @FunctionalInterface is optional: it makes the compiler check the rule and documents the intent. java.util.function provides the standard ones, such as Function, Supplier, Consumer, Predicate, BiFunction, UnaryOperator and their primitive variants.
Why this question is asked
Lambdas are everywhere in modern Java, and every one of them needs a target type: a functional interface that tells the compiler what the lambda's parameters and return type are. Interviewers ask this question because the details, such as why Comparator still counts and what the annotation really does, show whether we understand how lambdas fit into Java's type system rather than just how to write them. In this answer, we will recap how a lambda finds its type, state the counting rule precisely, check Comparator against it, see what @FunctionalInterface changes, and tour the java.util.function package.
1A Quick Recap: How a Lambda Gets Its Type
A lambda such as (a, b) -> a - b has no type of its own. The compiler gives it a type from the context where it appears, called the target type. If we assign it to a Comparator<Integer>, it becomes the implementation of Comparator's compare method; if we assign the same text to a BinaryOperator<Integer>, it implements apply instead. For this to work, the target interface must have exactly one method the lambda can implement, so the compiler knows which one it is.
2The Counting Rule
A functional interface has exactly one abstract method. When counting, three kinds of methods are left out. Default methods are not abstract, since they have bodies. Static methods belong to the interface, not to instances. And abstract methods whose signature matches a public method of java.lang.Object, such as equals(Object), hashCode() or toString(), are not counted either. The reason for the last rule is that every implementing object already inherits an implementation of those from Object, so a lambda never needs to supply them.
Abstract methods inherited from superinterfaces do count, but if several have the same signature they count as one. So an interface can extend another functional interface and stay functional, as long as it does not add a second abstract method.
| Method in the interface | Counts? | Why |
|---|---|---|
int compare(T a, T b) | Yes | Abstract, not from Object |
boolean equals(Object o) | No | Matches a public Object method |
default Comparator<T> reversed() | No | Has a body |
static Comparator<T> naturalOrder() | No | Belongs to the interface |
Object clone() | Yes | Object.clone is protected, not public |
3Checking Comparator Against the Rule
Comparator declares two abstract methods in its source: compare(T, T) and equals(Object). It redeclares equals only to document the contract for comparators that override it. Because equals(Object) matches the public Object.equals, it is not counted. The many methods added in Java 8, such as reversed, thenComparing, comparing and naturalOrder, are default or static. That leaves compare as the single abstract method, so Comparator is functional and lambdas work.
Notice the last example mixes all three kinds: comparing is a static method, String::length is a method reference implementing Function, and reversed is a default method returning a new Comparator. Only compare had to be supplied, and the lambda inside comparing supplies it.
4What @FunctionalInterface Does
The annotation is optional. Any interface with one abstract method is functional and can be the target of lambdas, whether it is annotated or not; Runnable and Comparator worked with lambdas from Java 8 onwards because they already had one abstract method. What the annotation adds is a compile-time check and a statement of intent.
Without the annotation, adding describe() would compile, and the error would appear far away, in every place that passes a lambda as a Discount. With it, the author sees the problem immediately, and readers know the interface is meant for lambdas and must stay that way. That makes the annotation worth adding to every interface we design for lambdas.
5The Standard Interfaces in java.util.function
Before defining our own, we should know the standard set, because JDK APIs such as streams and Optional use them. The four basic shapes are Function<T, R>, which takes a T and returns an R; Supplier<T>, which takes nothing and returns a T; Consumer<T>, which takes a T and returns nothing; and Predicate<T>, which takes a T and returns a boolean.
Around these are variants. BiFunction, BiConsumer and BiPredicate take two arguments. UnaryOperator<T> is a Function<T, T>, and BinaryOperator<T> is a BiFunction<T, T, T>. And there are primitive specialisations, such as IntPredicate, LongFunction<R>, ToIntFunction<T> and IntBinaryOperator, which avoid boxing an int into an Integer on every call.
6Follow-up questions
Can a functional interface have generic methods?
The single abstract method itself cannot be generic if we want to use a lambda, because a lambda cannot declare type parameters. The interface can be generic, like Function<T, R>, and a method reference can still target an interface whose abstract method is generic, but lambdas cannot.
Is a lambda an anonymous inner class?
No. A lambda is compiled to a private method in the enclosing class, and at run time invokedynamic creates an object that implements the functional interface and calls that method. Unlike an anonymous class, a lambda has no this of its own: this inside a lambda refers to the enclosing instance.
Why does an abstract clone() method count but equals does not?
The exclusion only covers methods that match public methods of Object. Object.clone() and Object.finalize() are protected, so an abstract clone() in an interface is not already implemented for every object in a way the interface can rely on, and it counts as a second abstract method.
7Conclusion
In this answer, we saw that a functional interface has exactly one abstract method, which is what a lambda or method reference implements. Default and static methods are not counted, and neither are abstract methods matching public Object methods, which is why Comparator, with compare and equals, is still functional. @FunctionalInterface is optional, but it makes the compiler enforce the rule and documents intent. And java.util.function supplies Function, Supplier, Consumer, Predicate, the two-argument and operator variants, and primitive specialisations that avoid boxing.
Open with
A functional interface is one with exactly one abstract method, because that's the method a lambda or method reference implements; the lambda gets its type from the target interface.
Then, if they want more
When counting, default methods don't count since they have bodies, static methods don't count, and abstract methods that match public methods of Object, like equals, hashCode and toString, don't count either, because every object already has implementations of them.
So Comparator is functional even though its source declares both compare and equals. equals is there only to document the contract, it matches Object.equals, and all the newer methods like reversed and comparing are default or static. That leaves compare. @FunctionalInterface is optional. Runnable and Comparator worked with lambdas without needing changes.
What it does is make the compiler check the rule, so if someone adds a second abstract method, the interface itself fails to compile instead of every lambda call site, and it tells readers the interface is meant for lambdas.
In java.util.function the basic shapes are Function, which maps T to R, Supplier, which produces a value, Consumer, which accepts one and returns nothing, and Predicate, which returns a boolean. Then there are two-argument versions like BiFunction, UnaryOperator and BinaryOperator for same-type cases, and primitive specialisations like IntPredicate or ToLongFunction, which avoid boxing.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What makes an interface functional? Is Comparator a functional interface even though it...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/c2537b6772/