Functional Interface – Java8

Functional Interface Java8 1200x630

Before Java 8, an interface could only declare one or more methods also known as Abstract Method (method with no implementation, just the signature). Starting with Java 8 an interface can also have implementation of one or more methods (knows as Interface Default Method) and static methods along with abstract methods. Interface Default Methods are marked default keyword.

So the question is, what is Functional Interface?

An interface with Single Abstract Method (SAM) is called Functional Interface.

Which means –

  1. An interface with Single Abstract Method is a Functional Interface
  2. An interface with Single Abstract Method and zero or more default methods and zero or more static method is also a valid Functional Interface.

Below is a functional interface with Single Abstract Method

Greeter.java
package com.readtorakesh.java8.functional_interface;

@FunctionalInterface
public interface Greeter {
    //Abstract method
    String greet(String name);
}

Below is a functional interface with Single Abstract Method along with default method and static method.

DefaultGreeter.java
package com.readtorakesh.java8.functional_interface;

@FunctionalInterface
public interface DefaultGreeter {
    //Abstract method
    String greet(String name);
    
    //Default method
    default String getGreeting() {
        return "Hello";
    }
    
    //Static method
    static String suffix() {
        return ":-)";
    }
}

You must have noticed the @FunctionalInterface annotation. What does it mean?

It is optional to annotate a function interface with @FunctionalInterface annotation. The @FunctionalInterface annotation enforces only single abstract method in the interface. If a developer adds additional abstract method in an interface annotated @FunctionalInterface compiler gives error. I recommend using @FunctionalInterface to clearly mark an interface as Functional Interface.

Functional Interface is base of Lambda Expression in Java 8. Lambda Expression can provide implementation of only a Functional Interface. It can not implement any other interface .

Below is Lambda Expression implementation of Greeter functional interface. It is implementing the one and only abstract method of the interface, which takes a String as input and returns a String.

Greeter greeter = o -> "Hi " + o;

Below is Lambda Expression implementation of DefaultGreeter functional interface. It is implementing the one and only abstract method of the interface, which takes a String as input and returns a String. Note that Lambda is using Static method defined in the interface.

DefaultGreeter defaultGreeter = o -> "Hi " + o + " " + DefaultGreeter.suffix();

Here is the Main application class, which is defining Lambda’s and executing them.

MainApp.java
package com.readtorakesh.java8.functional_interface;

public class MainApp {
    public static void main(String[] args) {
        System.out.println("Greeter Lambda");
        System.out.println("------------------");
        Greeter greeter = o -> "Hi " + o;
        System.out.println(greeter.greet("Rakesh"));

        System.out.println("\n");

        System.out.println("DefaultGreeter Lambda");
        System.out.println("------------------");
        DefaultGreeter defaultGreeter = o -> "Hi " + o + " " + DefaultGreeter.suffix();
        System.out.println(defaultGreeter.greet("Rakesh"));
    }
}

Here is output of the main application.

Greeter Lambda
------------------
Hi Rakesh


DefaultGreeter Lambda
------------------
Hi Rakesh :-)

Download Code

You can download complete source code referred in this article with gradle project here. https://github.com/rakeshprajapati1982/functional-interface

Further Reading

Please share it and help others if you found this blog helpful. Feedback, questions and comments are always welcome.

Comments