← Back to Java

Functional Interfaces

JavaMidjava

The Question

What are functional interfaces in Java?

What a Strong Answer Covers

  • exactly one abstract method
  • target for lambdas
  • @FunctionalInterface annotation
  • can have default/static methods
  • Predicate/Function/Consumer/Supplier built-ins
  • java.util.function

Senior-Level Answer

A functional interface in Java is an interface that declares exactly one abstract method. This single-abstract-method (SAM) constraint makes the interface usable as the target type for a lambda expression or method reference, enabling functional-style programming introduced in Java 8.

The @FunctionalInterface annotation is optional but strongly recommended. It instructs the compiler to enforce the SAM constraint and signals intent to readers. If you accidentally add a second abstract method, the compiler emits an error rather than silently breaking all lambda assignments.

The java.util.function package ships with a rich set of built-in functional interfaces covering the most common patterns. Predicate<T> takes a T and returns boolean (testing/filtering). Function<T, R> maps T to R (transformation). Consumer<T> takes a T and returns void (side effects). Supplier<T> takes nothing and returns T (factories/lazy evaluation). BinaryOperator<T> takes two T arguments and returns T (reduction). Primitive specializations like IntPredicate and LongSupplier avoid autoboxing overhead.

Functional interfaces can have any number of default and static methods — only abstract methods count toward the SAM constraint. For example, Predicate<T> has default methods and(), or(), and negate() that compose predicates.

Before Java 8, single-method interfaces like Runnable and Comparator were already widely used in anonymous inner classes. Lambda expressions are essentially a syntactic shorthand for anonymous implementations of functional interfaces — the compiler infers the target type from context.

A key design implication: when writing an API that accepts behavior as a parameter (callbacks, strategies, transformations), prefer accepting a standard functional interface from java.util.function rather than defining a custom one. This integrates naturally with the streams API and keeps the method signature immediately recognizable to Java developers.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly defines the SAM constraint, mentions @FunctionalInterface annotation, and names at least two built-in examples from java.util.function.

3/3 — Strong Answer

Adds that default/static methods don't count toward SAM, connects functional interfaces to lambda target types, names Predicate/Function/Consumer/Supplier with their signatures, and mentions pre-Java 8 examples like Runnable.

Common Mistakes

  • Saying @FunctionalInterface is required — it is optional; the interface is functional regardless of the annotation.
  • Not knowing the signatures of the core java.util.function types, especially confusing Consumer and Supplier.
  • Forgetting that default methods do not violate the SAM constraint, so Predicate with and()/or()/negate() is still functional.

Follow-Up Questions

  • Can a functional interface extend another interface? What constraints apply? — Yes — it can extend an interface as long as the total count of inherited abstract methods remains one, or the parent's abstract method is the same as the child's.
  • What is the difference between Function<T,R> and UnaryOperator<T>? — UnaryOperator<T> extends Function<T,T> — it's a specialization where input and output types are the same, reducing verbosity.
  • How does the compiler resolve which functional interface a lambda targets when multiple interfaces are in scope? — Target type inference — the compiler uses the expected type from the assignment, method parameter, or cast to determine which SAM to implement.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout