HomeCore JavaJava 8 - functional interface

Java 8 – functional interface

In this tutorial, we will discuss what is Java 8 Functional Interface, and how to use it in Java with several examples.

1. What is java Functional interface?

A Java functional interface is an interface that contains exactly one abstract method. It is introduced in Java 8 to support the Java Lambda Expression.

  1. An Interface that contains exactly one abstract method is known as functional interface. It can have any number of defaultstatic methods but can contain only one abstract method.
  2. Functional Interface also known as Single Abstract Method Interfaces or SAM Interfaces. It is a new feature in Java 8, which helps to achieve functional programming approach.

1.1. Example 1

interface MyInterface1 {
 
    public void show();
 
}

1.2. Example 2

If we use @FunctionalInterface, it make sure there should be only one abstract method within that interface, otherwise will get compiler error.

@FunctionalInterface
interface MyInterface2 {
 
    public void show();
 
}

We can use lambda expression to implement Functional Interface methods. Let’s see a example.

public class LmbdaTest {

	public static void main(String[] args) {
		
		MyInterface1 f1 = () -> System.out.println("It's Test Time!");
		f1.show();
		
		MyInterface2 f2 = () -> System.out.println("It's My Test!");
		f2.show();

	}
}

Results:

It's Test Time!
It's My Test!

2. Java built-in functional interfaces

  1. The java.util.function package contains general purpose functional interfaces used by JDK and also available for end users like us. The interfaces defined in this package are annotated with @FunctionalInterface annotation.
  2. Since java 1.8 onward following interfaces are functional interface, all these interfaces annotated with @FunctionalInterface.

1. Runnable –> It contains only run() method
2. Comparable –> It contains only compareTo() method
3. ActionListener –> It contains only actionPerformed()
4. Callable –> It contains only call() method.

Following are the few important Functional Interfaces in java.util.function package.

PredicateRepresents a predicate (boolean-valued function) of one argument.
SupplierRepresents a supplier of results. Takes no argument and return a result.
Function<T,R>Represents a function that accepts one argument and produces a result.
ConsumerRepresents an operation that accepts a single input argument and returns no result.
BiConsumer<T,U>Represents an operation that accepts two input arguments and returns no result.
BiFunction<T,U,R>Represents a function that accepts two arguments and produces a result.
BiPredicate<T,U> Represents a predicate (boolean-valued function) of two arguments.

3. @FunctionalInterface annotation

  1. This annotation will be used to define Functional interface. This annotation is not the requirement for the java compiler to determine the interface is an functional interface but it helps the compiler to identify the accidental violation of the our design intent.
  2. This annotation will be very much useful for us while creating our custom functional interfaces. @FunctionalInterface  is used to verify the interface follows all of the rules that can make this interface as functional interface.
  3. We can create functional interface without using @FunctionalInterface, but if we define multiple abstract methods compiler won’t throw any error while creating interface. If you define interface using @FunctionalInterface, it ensures the interface should contain single abstract method.

4. @FunctionalInterface Rules

  1. Inside Functional Interface we can take only one abstract method, if we take more than one abstract method then compiler raise an error.
  2. Interface can declares an abstract method overriding one of the public method from java.lang.Object, that still can be considered as functional interface. The reason is any implementation class to this interface will have implementation for this abstract method either from super class (bare minimum java.lang.Object) class or defined by implementation class it self. In the below example toString() method declared as abstract which will be implemented in its concrete implementation class or at last derived from java.lang.Object class.

4.1. Example :

@FunctionalInterface
interface  MyInterface {
    
    abstract void show();
 
    @Override
    String toString();
 
    default void print() {
        System.out.println(" Default Method : allowed since java 1.8 ");
    }
 
    static void update() {
        System.out.println("Static Method : allowed since java 1.8 ");
    }
}

5. Functional Interface with respect to Inheritance

  1. If an interface extends java Functional Interface and child interface doesn’t contain any abstract method
    then child interface is also Functional Interface. In this case In child interface we can’t define any new abstract methods
  2. In the child interface we can define exactly same parent interface abstract method.
@FunctionalInterface
interface  MyInterface {
    
    abstract void show();
    
}
@FunctionalInterface
interface  MyChildInterface1 extends MyInterface {
    
    abstract void show();
 
}
@FunctionalInterface
interface  MyChildInterface2 extends MyInterface {
    
 
}

6. References

  1. Java document
  2. java.util.function package java document
  3. Java 8 Lambda Expression

LEAVE A REPLY

Please enter your comment!
Please enter your name here