Java Annotation @FunctionalInterface
The @FunctionalInterface
annotation is a standard Java annotation that is used to indicate that an interface is intended to be a functional interface.
A functional interface is an interface that has only one abstract method, and is therefore suitable for use with lambda expressions and method references. By marking an interface with the @FunctionalInterface
annotation, you are making it clear that the interface is intended to be used as a functional interface, and that it should have only one abstract method.
Here is an example of how to use the @FunctionalInterface
annotation:
@FunctionalInterface public interface MyInterface { void myMethod(); }
In this example, the MyInterface
interface is annotated with @FunctionalInterface
to indicate that it is intended to be used as a functional interface. The interface has only one abstract method, myMethod()
, which makes it suitable for use with lambda expressions and method references.
When you use the @FunctionalInterface
annotation, it is important to ensure that your interface actually has only one abstract method. If you add additional abstract methods to a functional interface, you will receive a compile-time error. By using the @FunctionalInterface
annotation, you can help to ensure that your code is clean, concise, and easy to understand.