Java - FunctionalInterface Annotation Type

Introduction

An interface with one abstract method is a functional interface.

@FunctionalInterface marks interfaces which contain one and only one abstract method.

A compile-time error is generated if the interfaces annotated with this annotation than have more then one abstract method.

The FunctionalInterface annotation type is a marker interface.

Example

The following Runner interface uses a @FunctionalInterface annotation.

@FunctionalInterface
interface Runner {
        void run();
}

The following Job interface marked by @FunctionalInterface annotation will generate a compile-time error because the Job interface declares two abstract methods.

@FunctionalInterface
interface Job {
        void run();
        void abort();
}

The following Test class marked by @FunctionalInterface annotation will generate a compile-time error because a @FunctionalInterface annotation can only be used on interfaces.

@FunctionalInterface
class Test {
   public void test() {
   }
}