Sobes.tech
Junior — Middle

Is it possible to define an interface without the @FunctionalInterface annotation as a functional interface?

sobes.tech AI

Answer from AI

The @FunctionalInterface annotation in Java is not mandatory for an interface to be considered functional. The main requirement is that the interface contains exactly one abstract method.

The annotation serves to explicitly declare and verify this fact at compile time, but even without it, an interface with a single abstract method can be used with lambda expressions and methods expecting a functional interface.

Example:

interface MyFunction {
    void apply(); // single abstract method
}

MyFunction f = () -> System.out.println("Hello");
f.apply();

Here, MyFunction is a functional interface without the annotation.

Is it possible to define an interface without the… - sobes.tech