Functional Interfaces

Functional Interfaces are a core concept introduced in Java 8 to support lambda expressions and functional programming. A functional interface is an interface that contains exactly one abstract method, although it can have multiple default or static methods.

Important Points about Functional Interfaces

  1. Single Abstract Method (SAM):
    A functional interface must have exactly one abstract method. This is why it is sometimes called a SAM interface.

  2. @FunctionalInterface Annotation:
    This annotation is optional but highly recommended. It tells the compiler to ensure the interface follows the rule of having only one abstract method.

@FunctionalInterface
interface MyFunction {
    void apply();
}Code language: CSS (css)

  3.Default and Static Methods Allowed:

    Functional interfaces can have default and static methods without affecting their status.

@FunctionalInterface
interface MyFunction {
    void apply();  // Abstract method

    default void greet() {
        System.out.println("Hello!");
    }

    static void info() {
        System.out.println("Functional Interface Example");
    }
}Code language: JavaScript (javascript)

4. Usage with Lambda Expressions:

Functional interfaces are the target types for lambda expressions.

MyFunction func = () -> System.out.println("Lambda executed!");
func.apply();  // Output: Lambda executed!Code language: JavaScript (javascript)

Common Built-in Functional Interfaces (in java.util.function package)

Interface Method Signature Use Case
Function<T, R> R apply(T t) Takes input T, returns output R
Predicate<T> boolean test(T t) Evaluates a condition
Consumer<T> void accept(T t) Performs action on input
Supplier<T> T get() Supplies a value without input
UnaryOperator<T> T apply(T t) A Function where input and output are same
BinaryOperator<T> T apply(T t1, T t2) Takes two inputs and returns same type

Program

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

public class FunctionalInterfaceDemo {
    public static void main(String[] args) {
        Calculator add = (a, b) -> a + b;
        Calculator multiply = (a, b) -> a * b;

        System.out.println("Addition: " + add.operation(5, 3));       // 8
        System.out.println("Multiplication: " + multiply.operation(5, 3)); // 15
    }
}

Common Errors

  • Having more than one abstract method will cause a compilation error if annotated with @FunctionalInterface.

  • Interface extending another functional interface must not add another abstract method.

Why Use Functional Interfaces?

  • Enable cleaner, more concise code using lambda expressions.

  • Useful in streams, event handling, asynchronous programming, and callback mechanisms.

  • Encourage declarative programming style.


Functional interfaces are a foundational feature in Java’s functional programming paradigm, introduced in Java 8. By restricting an interface to a single abstract method, Java enables powerful constructs like lambda expressions and method references, leading to more concise, readable, and maintainable code. They are extensively used in modern Java applications—especially with streams, event listeners, and API callbacks.

Scroll to Top