Functional Interfaces in Java 8 Explained

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(java.lang.FunctionalInterface) annotation
@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 @FunctionalInterface tag
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 –

Java 8 code showing usage of @FunctionalInterface tag
package com.javabrahman.java8;
@FunctionalInterface
public interface CustomFunctionalInterface {
  
  //Single abstract method  
  public void firstMethod();

}

Examples of functional interfaces
Lets see some examples of functional interfaces to understand them better. I have split the examples into 3 types –

  1. 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 CustomFunctionalInterface below –
    Example of custom Or user-defined Functional Interfaces
    package com.javabrahman.java8;
    public interface CustomFunctionalInterface {
        
      //This is the only abstract method.Hence, this
      //interface qualifies as a Functional Interface
      public void firstMethod();
    
    }
  2. 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.Runnable, java.lang.Callable<V>. Let use see the code used for defining these pre-existing functional interfaces.
    Example of pre-existing functional interfaces in Java
    //java.lang.Runnable
    @FunctionalInterface
    public interface Runnable {
        public abstract void run();
    }
    //java.lang.Callable<V>
    @FunctionalInterface
    public interface Callable<V> {
        V call() throws Exception;
    }
  3. Newly defined functional interfaces in Java 8 in java.util.function package – 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 –
    Example of new pre-defined functional interfaces in Java 8
    //java.util.function.Predicate<T>
    @FunctionalInterface
    public interface Predicate<T> {
        boolean test(T t);
    }

Conclusion
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.

 

Digiprove sealCopyright © 2014-2017 JavaBrahman.com, all rights reserved.
  • Amitava

    Good to start learning Java 8

  • Bob

    Exactly what I’m looking for!! Thank you

  • DarkLite1

    Great post! Thank you for the clear explanation.