Overview – In this tutorial we will be looking at one of the most fundamental features of functional aspects of Java 8 – functional interfaces. We will start by looking at the definition of functional interfaces and the primary purpose for which they have been added to Java 8. This will be followed by understanding the new
@FunctionalInterface annotation introduced in Java 8 and see an example explaining the definition of a custom functional interface. Lastly, we will look at examples showing the 3 types of functional interfaces which are available at the disposal of a programmer in Java 8.
What is a Functional Interface
A functional interface, introduced in Java 8, is an interface which has only a single abstract method. Conversely, if you have any interface which has only a single abstract method, then that will effectively be a functional interface. This interface can then be used anywhere where a functional interface is eligible to be used.
The primary purpose served by Functional Interfaces
One of the most important uses of Functional Interfaces is that implementations of their abstract method can be passed around as lambda expressionsRead Java 8 Lambda Expressions Tutorial. By virtue of their ability to pass around functionality(i.e. behavior), functional interfaces primarily enable behavior parameterization.
@FunctionalInterface annotation can be used to explicitely specify that a given interface is to be treated as a functional interface. Then the compiler would check and give a compile-time error in case the annotated interface does not satisfy the basic condition of qualifying as a functional interface(that of having a single abstract method).
Please note that
@FunctionalInterface is an informative annotation, i.e. it is not mandatory to use this annotation for classifying the given interface as a valid functional interface. Rather, if one uses this annotation, then the compiler ensures that the interface is not inadvertently changed in such a way that it no longer remains a functional interface
Example of using the
The code below shows the same
CustomFunctionalInterface defined above with just the
@FunctionalInterface annotation added on top of it to demonstrate the use of the annotation –
Examples of functional interfaces
Lets see some examples of functional interfaces to understand them better. I have split the examples into 3 types –
- Custom Or User defined Functional Interfaces – These are interfaces defined by the user and have a single abstract method. These may/may not be annotated by
@FunctionalInterface. Let us see the code for a user-defined functional interface named
- Pre-existing functional interfaces in Java prior to Java 8 – These are interfaces which already exist in Java Language Specification and have a single abstract method. Eg.
java.lang.Callable<V>. Let use see the code used for defining these pre-existing functional interfaces.
- Newly defined functional interfaces in Java 8 in
java.util.functionpackage – These are pre-defined Functional Interfaces introduced in Java 8. They are defined with generic types and are re-usable for specific use cases. One such Functional Interface is the
Predicate<T>interface which is defined as follows –
In this tutorial we understood functional interfaces, their primary purpose, learnt the new
@FunctionalInterface annotation introduced in Java 8 and then finally saw examples of various types of functional interfaces available in Java 8.