1. Home
  2. Interview Questions
  3. Java
  4. 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

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best 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.Function has andThen and compose as defaults and is still functional.
  • Static methods do not count. Comparator.comparing is static.
  • Abstract methods that match public methods of java.lang.Object do not count. Comparator declares boolean equals(Object obj), but every implementing class already inherits equals from Object. So compare is 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, Callable and Comparator existed 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 @FunctionalInterface is required for lambdas to work. It is only a compiler check and a piece of documentation.
  • Forgetting that redeclared Object methods like equals are excluded. This is why Comparator looks like it has two abstract methods but is functional.
  • Writing custom interfaces such as StringChecker when Predicate<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 IntFunction or ToIntFunction over Function<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

All easy questions →

Related Questions

‹
Q16 of 18 · Java
Next ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA