Java 8 – What is functional Interface?

Spread the love

Functional interface is a new addition to Java 8. In simple terms, an interface which has one single abstract method is qualified as Functional interface. The built-in classes in JDK are full of examples of Functional Interfaces(Runnable, Callable, ActionListener). Java 8 formalized such interface definition with @FunctionalInterface annotation. FunctionalInterface is also useful while creating lambda expressions. Those who are not familiar with Lambda expression, it was again a new feature introduced in Java 8. Lambda helps developers to write concise code. Its expressive syntax saves developer’s time. Before Lambda, for every single method declaration of an interface, developers had to create Class. Eventually, an application source code is filled with many small classes which just provide an implementation for one single method. FunctionalInterface and Lambda expressions are considered to be best practice for writing neat code. Lets have a look at how JavaDoc describes FunctionalInterface

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.

What we can derive from the above definition is that (Remember these points, it’s helpful to describe FunctionalInterface in Java Interview).

  • FunctionalInterface has one and only one single abstract method.
  • FunctionalInteface can have many default methods with implementation.
  • FunctionalInterface if overrides java.lang.Object method that cannot be counted as abstract method.

FunctionalInteface is also popularly known as Single Abstract Method (SAM). Now that we have understood what FunctionalInterface is, let’s explore some examples.

@FunctionalInterface
interface Printer {
	void print(String contentToPrint);
}

In above code, an interface is annotated with @FunctionalInterface. Note that the interface definition has one single abstract method.

public class DummySystem{
  public static void main(String[] args){
    DummySystem system = new DummySystem();
    system.printContent("Hello", (content)-> System.out.println("Printed on Dot matrix printer: " + content));
    system.printContent("World", (content)-> System.out.println("Printed on Laser printer: " + content));
    system.printContent("Lambda is fun!!!", (content)-> System.out.println("Printed on Inkjet printer: " + content));
  }

  public void printContent(String content, Printer printer){
    printer.print(content);
  }
}

Note how easy it is to provide different implementations for each Printer. Prior to Java 8, this would require either create a class for each implementation or anonymous inner class. With Lambda and FunctionalInterface, the code is now concise plus expressive. You may have a question – Should all existing interfaces (SAM) required to be modified to use with Lambda? Java 8 compiler recognizes all the existing interfaces which have one single abstract method as FunctionalInterface. Hence annotating an interface with @FunctionalInterface is optional, but considered to be a best practice. Now that we have understood how FunctionalInterface work, lets have look at the existing implementations in Java

  • java.lang.Runnable
  • java.awt.event.ActionListener
  • java.util.concurrent.Callable

There are more, but we use above interfaces frequently. With FunctionalInterface, implementation for each of these interfaces is very concise.

Runnable r = () -> System.out.println("You are inside new thread.");

Or, if you want to listen to button event

Button b = new Button("Click Me!");
b.addActionListener((e)-> System.out.println("Did you just click me?"));

In the next article, we will dive deep into other FunctionalInterface available in Java 8 and some glimpse of Functional Programming.

Leave a Comment.