Skip to content

1. 📖 Definition

A Functional Interface in Java is an interface that contains exactly one abstract method (SAM – Single Abstract Method). It can have multiple default and static methods, but only one abstract method.

Annotated using @FunctionalInterface (optional but recommended).

Interview-ready answer: “A functional interface is an interface with a single abstract method, designed to be used as the target for lambda expressions.”


2. ❓ Why it is used

Without functional interfaces, lambdas have no target type. They act as the contract that defines what a lambda expression must implement.

Think of it as a shape that a lambda must fit into.

Interview-ready answer: “Functional interfaces are used to provide a target type for lambda expressions, enabling behavior to be passed as a parameter.”


3. 🕰️ Background / Need

Before Java 8:

  • Heavy use of anonymous inner classes
  • No clean way to pass behavior
  • Code was verbose and harder to maintain

Example problem:

new Thread(new Runnable() {
    public void run() {
        System.out.println("Task");
    }
}).start();

Java needed:

  • Cleaner syntax
  • Functional programming support
  • Better collection handling (Streams)

Interview-ready answer: “Before Java 8, we relied on verbose anonymous classes. Functional interfaces enabled lambda expressions to simplify this and support functional programming.”


4. 🧠 Core Idea / Working

Key Points:

  • A lambda expression implements a functional interface
  • The abstract method defines the method signature
  • Compiler matches lambda → abstract method

Example:

@FunctionalInterface
interface Calculator {
    int operate(int a, int b);
}

Lambda mapping:

Calculator add = (a, b) -> a + b;

Rules:

  • Only one abstract method
  • Can extend other interfaces (if still SAM)
  • Can include:

    • default methods
    • static methods

Behind the scenes:

  • Uses type inference
  • Implemented via invokedynamic

Interview-ready answer: “A functional interface defines a single abstract method, and the lambda expression provides its implementation, matched by the compiler.”


5. 🧩 Variants / Types

You’re thinking wrong if you treat all functional interfaces the same. There are standard categories:

1. Predicate (boolean condition)

Predicate<Integer> p = x -> x > 10;

2. Function (input → output)

Function<Integer, String> f = x -> "Value: " + x;

3. Consumer (takes input, no return)

Consumer<String> c = x -> System.out.println(x);

4. Supplier (no input, returns output)

Supplier<Double> s = () -> Math.random();

5. Custom Functional Interface

@FunctionalInterface
interface MyFunc {
    void execute();
}

Interview-ready answer: “Functional interfaces are commonly categorized as Predicate, Function, Consumer, and Supplier based on input-output behavior.”


6. ⚖️ Advantages

  • Enables lambda expressions
  • Reduces boilerplate code
  • Improves readability
  • Promotes functional programming
  • Reusable behavior contracts

Interview-ready answer: “Functional interfaces enable lambda expressions, reduce boilerplate code, and support cleaner, more declarative programming.”


7. ⚠️ Limitations / Disadvantages

  • Only one abstract method allowed
  • Can become confusing if overused
  • Poor readability with complex lambdas
  • Not suitable for multi-step logic

Common mistake: Trying to force everything into functional style.

Interview-ready answer: “Functional interfaces are limited to a single abstract method and can reduce readability if overused with complex logic.”


8. 💻 Code Examples

Example 1: Custom Functional Interface

@FunctionalInterface
interface Greeting {
    void sayHello();
}

// Lambda implementation
Greeting g = () -> System.out.println("Hello");
g.sayHello();

Example 2: Built-in Function Interface

import java.util.function.Function;

Function<Integer, Integer> square = x -> x * x;

System.out.println(square.apply(5)); // Output: 25

Example 3: Predicate with Filtering

import java.util.*;
import java.util.function.Predicate;

List<Integer> list = Arrays.asList(1, 2, 3, 4);

Predicate<Integer> isEven = x -> x % 2 == 0;

list.stream()
    .filter(isEven) // using functional interface
    .forEach(System.out::println);

How to explain this in interview: “I defined a functional interface and implemented it using a lambda. In real scenarios, I use built-in interfaces like Predicate and Function with streams.”


9. 🌍 Real-World Use Cases

  • Filtering data (Predicate)
  • Transforming data (Function)
  • Logging/printing (Consumer)
  • Lazy value generation (Supplier)
  • Event handling (UI frameworks)
  • Multithreading (Runnable)

Interview-ready answer: “Functional interfaces are used in streams, filtering, transformations, event handling, and asynchronous programming.”


10. 🧪 Practical Scenario

Problem:

You want to apply different discount strategies dynamically.

Solution:

@FunctionalInterface
interface Discount {
    double apply(double price);
}

// Different strategies
Discount newYear = price -> price * 0.8;
Discount regular = price -> price * 0.95;

double finalPrice = newYear.apply(1000);
System.out.println(finalPrice);

Why this works:

  • Behavior is pluggable
  • No need for multiple classes

How to explain this in interview: “I used a functional interface to represent a discount strategy and implemented it using lambdas, making the behavior flexible and reusable.”


11. 🚀 Interview Tips

Common Questions:

  • What is a functional interface?
  • Why only one abstract method?
  • Difference between functional interface and normal interface?
  • What is @FunctionalInterface?

Edge Cases:

  • Can it have multiple default methods? → YES
  • Can it extend another interface? → YES (if still SAM)
  • What happens if 2 abstract methods? → Compilation error

Mistakes:

  • Forgetting SAM rule
  • Confusing lambda with functional interface
  • Not knowing built-in interfaces (Predicate, Function, etc.)

12. 📝 Summary

  • Functional Interface = Single Abstract Method
  • Backbone of lambda expressions
  • Can have default & static methods
  • Built-in types: Predicate, Function, Consumer, Supplier
  • Used heavily in Streams API

30-second interview pitch: “A functional interface in Java is an interface with a single abstract method, used as the target for lambda expressions. It enables passing behavior as a parameter and is widely used in streams through built-in interfaces like Predicate and Function.”


🔥 Mock Interview Q&A (with grilling)

Q1: What is a functional interface?

Answer: An interface with exactly one abstract method used for lambda expressions.

👉 Follow-up: Can it have multiple methods? Yes—but only one abstract method. Others must be default or static.


Q2: Why only one abstract method?

If you don’t know this, you don’t understand lambdas.

Answer: Because lambda expressions map to a single method implementation. Multiple methods create ambiguity.

👉 Follow-up: Then how does Comparator work? It has one abstract method (compare) and multiple default methods.


Q3: Is Runnable a functional interface?

Answer: Yes, because it has only one abstract method run().

👉 Follow-up: Then why no @FunctionalInterface? It predates Java 8. Annotation is optional.


Q4: Functional Interface vs Normal Interface?

Answer: Functional interface → one abstract method Normal interface → multiple abstract methods allowed

👉 Follow-up: Then why not always use functional? Because complex contracts need multiple methods.


Q5: What if I add a second abstract method?

Answer: Compilation error if annotated with @FunctionalInterface.

👉 Follow-up: Without annotation? It compiles but is no longer a functional interface—lambda won’t work.