← Back to Home

Functional Interfaces

A functional interface is the foundation of lambda expressions in Java. It represents a single unit of behavior and enables functional programming constructs introduced in Java 8.

This is a very high-frequency interview topic, tightly coupled with lambdas, streams, and method references.

What Is a Functional Interface?

A functional interface is an interface that has exactly one abstract method.

@FunctionalInterface
public interface Runnable {
    void run();
}
          
  • Can have any number of default and static methods
  • Must have only one abstract method

Why Functional Interfaces Were Introduced

  • Enable lambda expressions
  • Reduce boilerplate code
  • Support functional programming style
  • Improve readability and expressiveness

Rules of Functional Interfaces (Interview Must-Know)

  1. Exactly one abstract method
  2. Can contain:
    • default methods
    • static methods
  3. Can override Object class methods
  4. @FunctionalInterface annotation is optional but recommended

Example: Custom Functional Interface

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

Valid functional interface.

Using Functional Interface with Lambda

Calculator c = (a, b) -> a + b;
System.out.println(c.add(10, 20));
          

Lambda implements the abstract method.

Invalid Functional Interface Example

@FunctionalInterface
interface Invalid {
    void m1();
    void m2(); // ❌ Compilation error
}
          

Reason: more than one abstract method.

Default & Static Methods in Functional Interface

@FunctionalInterface
interface Demo {
    void show();

    default void info() {
        System.out.println("Default method");
    }

    static void help() {
        System.out.println("Static method");
    }
}
          
  • Still functional
  • Only one abstract method matters

Built-in Functional Interfaces (Very Important)

Java provides many functional interfaces in java.util.function.

1️⃣ Predicate<T>

  • Takes one input
  • Returns boolean
  • Used for conditions / filtering
Predicate<Integer> isEven = n -> n % 2 == 0;
          

Method:

boolean test(T t);
          

2️⃣ Function<T, R>

  • Takes one input
  • Returns one output
Function<String, Integer> length = s -> s.length();
          

Method:

R apply(T t);
          

3️⃣ Consumer<T>

  • Takes one input
  • Returns nothing
Consumer<String> print = s -> System.out.println(s);
          

Method:

void accept(T t);
          

4️⃣ Supplier<T>

  • Takes no input
  • Returns output
Supplier<Double> random = () -> Math.random();
          

Method:

T get();
          

5️⃣ Runnable (Classic Functional Interface)

Runnable r = () -> System.out.println("Thread running");
          

Method:

void run();
          

Primitive Functional Interfaces (Performance-Oriented)

Avoid boxing/unboxing.

Interface Example
IntPredicate int → boolean
IntFunction<R> int → R
IntConsumer int → void
IntSupplier → int

Used in performance-critical code.

Functional Interface vs Normal Interface

Aspect Functional Interface Normal Interface
Abstract methods Exactly 1 Multiple
Lambda support ✔ Yes ❌ No
Java version 8+ All
Usage Behavior Contract

Functional Interface vs Abstract Class

Aspect Functional Interface Abstract Class
Inheritance Multiple allowed Single
State ❌ No ✔ Yes
Lambda support ✔ Yes ❌ No
Constructor ❌ No ✔ Yes

Functional Interface & Method References (Preview)

Consumer<String> c = System.out::println;
          
  • Cleaner alternative to lambdas
  • Requires functional interface

Common Beginner Mistakes

  • Adding multiple abstract methods
  • Forgetting functional interface requirement
  • Overusing lambdas for complex logic
  • Confusing default methods with abstract methods
  • Not using built-in interfaces when available

Best Practices (Interview + Real World)

  • Use built-in functional interfaces whenever possible
  • Mark custom ones with @FunctionalInterface
  • Keep lambdas short and readable
  • Prefer method references when clearer
  • Avoid state inside lambdas

Interview-Ready Answers

Short Answer

A functional interface is an interface with exactly one abstract method.

Detailed Answer

In Java, a functional interface is an interface that contains a single abstract method and can be implemented using lambda expressions. It enables functional programming in Java and includes built-in interfaces like Predicate, Function, Consumer, and Supplier.

Key Takeaway

Functional Interfaces enable lambdas. They are the backbone of modern Java, powering streams, concurrency, and functional programming.