Course contentsShow
← Java
Lesson 1632 of 2,87035. Functional InterfacesFree lesson

What the @FunctionalInterface Annotation Does

Learn how @FunctionalInterface marks interfaces intended to have exactly one abstract method and enables compiler checks.

What the @FunctionalInterface Annotation Does

You'll learn how @FunctionalInterface acts as both documentation and a compiler safety net for functional interfaces.

The Core Purpose

The @FunctionalInterface annotation is an optional marker you place on an interface to explicitly declare your intention that it should have exactly one abstract method. Think of it like a quality control stamp—it doesn't change what the interface does, but it tells the compiler to verify that your interface follows the functional interface rules.

Why Use It?

While any interface with a single abstract method can work as a functional interface (with or without the annotation), adding @FunctionalInterface provides two key benefits:

1. Compiler Enforcement

If someone later tries to add a second abstract method to your annotated interface, the compiler immediately raises an error. This prevents accidental breakage of code that uses your interface with lambdas.

2. Clear Intent Documentation

When other developers see @FunctionalInterface, they immediately understand: "This interface is designed to be implemented with lambdas or method references."

How It Works

@FunctionalInterface
public interface StringTransformer {
    String transform(String input);
    
    // Default and static methods are still allowed
    default String transformTwice(String input) {
        return transform(transform(input));
    }
}

// Compiler error if you try this:
// @FunctionalInterface
// public interface Broken {
//     void method1();
//     void method2();  // ERROR: Multiple abstract methods!
// }

The annotation is purely compile-time validation—it has no runtime effect.

Key Takeaway: @FunctionalInterface is an optional but recommended annotation that ensures your interface stays functional (single abstract method) and clearly communicates your design intent to both the compiler and other developers.