@FunctionalInterface Annotation in Java 8

Java 8 introduces functional interfaces. If an interface has only one abstract method, then it is known as the functional interface. This can be annotated with @FunctionalInterface annotation. The following are some of the points to remember about the functional interfaces:

  • Functional interface has only one abstract method
  • @FunctionalInterface annotation can be used for denoting the functional interface. However, it is not necessary to use this annotation, it is only useful to catch the error in the compile time. When you annotate an interface with @FunctionalInterface, if more then one abstract method is defined it will throw a compiler error.
  • Functional interfaces can define one or more default methods.
  • Lambda expressions can be used for invoking the functional interface methods. You can look the example in our previous tutorial.

It is defined as:

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!