Java - @FunctionalInterface Annotation

Introduction

The functional interface may optionally be annotated with the annotation @FunctionalInterface.

The presence of @FunctionalInterface Annotation tells the compiler to make sure that the declared type is a functional interface.

If the annotation @FunctionalInterface is used on a non-functional interface or other types such as classes, a compile-time error occurs.

If you do not use the annotation @FunctionalInterface on an interface with one abstract method, the interface is still a functional interface.

Example

The following Operations interface will not compile, as the interface declaration uses the @FunctionalInterface annotation and it is not a functional interface:

@FunctionalInterface
public interface Operations {
        double add(double n1, double n2);
        double subtract(double n1, double n2);
}

To compile the Operations interface, either remove one of the two abstract methods or remove the @FunctionalInterface annotation.

The following declaration for a Test class will not compile, as @FunctionalInterface cannot be used on a type other than a functional interface:

@FunctionalInterface
public class Test {
        // Code goes here
}

Related Topic