Best practice of using @FunctionalInterface annotation

Lambda expressions and Functional programming are considered as the cornerstones of the Java 8 .

what is a Lambda Expression?

simply a block of logic that get passed around , like an anonymous method.

What is a Functional Interface?

simply a functional interface is an interface with a single abstract method.

Functional interfaces are used as the basis of the lambda expression and that is how functional interface technically related to the lambda expressions.

How compiler works on making Functional Interfaces

Java compiler is smart enough to take any interface with a single abstract method as a functional interface.

In other way round if a class has marked explicitly with the annotation and contains more than one abstract method or no abstract methods at all, then the compiler detects it as an error.

Best Practices of applying @FunctionalInterface annotation

As you see in the above description, truly its not a must to annotate explicitly in developing. so why would we do it explicitly ???

Why we need to explicitly add @FunctionalInterface ???

Here, now let’s consider that you are working on a project with a team, so in case if you are not using the annotation and even you have no idea of the annotation another developer might probably treat any interface that you have created with a single abstract method as a functional interface. If you modify the code later to have some other abstract methods, the code will break indeed and it will not work as a Functional interface.

Now we clearly knows why we need to annotate explicitly. Therefore other developers can clearly know for which interfaces that they can safely apply lambdas…

Tech enthusiast…</>