A functional interface is simply an interface that has one abstract method.

Introduced in Java 8, a functional interface is simply an interface that has exactly one abstract method. Learn more about functional interfaces in this tutorial.

Table Of Contents

1. What is a Functional Interface?

1.1. Only one abstract method is allowed

Functional interfaces are new additions in Java 8. As a rule, a functional interface can contain exactly one abstract method. These functional interfaces are also called Single Abstract Method interfaces (SAM Interfaces).

Apart from one abstract method, a functional interface can also have the following methods that do not count for defining it as a functional interface.

  • Default methods
  • Static methods
  • Public methods inherited from the Object class

1.2. Implemented by Lambda Expressions

In Java, lambda expressions can be used to represent an instance of a functional interface. For example, Comparator interface is a functional interface.

@FunctionalInterface
public interface Comparator {
	int compare(T o1, T o2);
	boolean equals(Object obj);

	//and multiple default methods...
}

Comparator interface has only two abstract methods compare() and equals(). But equals() has been inherited from the Object class, so it is not counted. Other than these two methods, all other methods are default methods. So Comparator is qualified to be declared as a functional interface.

Java program to implement Comparator using a lambda expression.

//Compare by Id
Comparator compareById = Comparator.comparing(e -> e.getId());

Comparator compareByFirstName = Comparator.comparing(e -> e.getFirstName());

2. @FunctionalInterface Annotation

Java 8 introduced the annotation

//Compare by Id
Comparator compareById = Comparator.comparing(e -> e.getId());

Comparator compareByFirstName = Comparator.comparing(e -> e.getFirstName());
0 to mark an interface as a functional interface. The primary use of this annotation is for compiler-level errors when the interface violates the contracts of precisely one abstract method.

Note that using the annotation @FunctionalInterface is optional.

If the interface has one abstract method and does not have @FunctionalInterface annotation, the interface is still a functional interface, and it can be the target type for lambda expressions.

The presence of the annotation protects us from inadvertently changing a functional interface into a non-functional interface, as the compiler will catch it.

Let’s build our first functional interface. Note that methods in an interface are, by default, abstract.

@FunctionalInterface
public interface MyFirstFunctionalInterface 
{
    public void firstWork();
}

Let’s try to add another abstract method:

@FunctionalInterface
public interface MyFirstFunctionalInterface 
{
    public void firstWork();
    public void doSomeMoreWork();   //error
}

The above code will result in a compiler error:

Unexpected @FunctionalInterface annotation
@FunctionalInterface ^ MyFirstFunctionalInterface is not a functional interface
multiple non-overriding abstract methods found in interface MyFirstFunctionalInterface

A functional interface is simply an interface that has one abstract method.

Read More : Generic Functional Interfaces

3. Functional Interfaces in JDK

The following is a list of Java’s most commonly used functional interfaces.

  • Runnable: contains only the run() method.
  • Comparable: contains only the compareTo() method.
  • ActionListener: contains only the actionPerformed() method.
  • Callable: contains only the call() method.
  • Predicate: a boolean-valued function that takes an argument and returns true or false.
  • BiPredicate: a predicate with two arguments.
  • Consumer: an operation that takes an argument, operates on it, and returns no result.
  • BiConsumer: a consumer with two arguments.
  • Supplier: a supplier that returns a value.
  • Function:  takes an argument of type T and returns a result of type R.
  • BiFunction: takes two arguments of types T and U and returns a result of type R.

4. Demo

Let’s see a quick example of creating and using functional interfaces in Java.

We are using a functional interface Function to create the formula for mathematical squares.

Function square = x -> x * x;

The Function interface has one abstract method

//Compare by Id
Comparator compareById = Comparator.comparing(e -> e.getId());

Comparator compareByFirstName = Comparator.comparing(e -> e.getFirstName());
1 that we have implemented above. we can execute the above method as follows:

System.out.println( square.apply(5) );  //Prints 25

5. Conclusion

In this tutorial, we learned to create and manage functional interfaces in Java. We learned that a functional interface has only one abstract method and they can be implemented by the lambda expressions.

We also saw the JDK provided existing functional interfaces, and finally how to create an use a functional interface.

Can we use object class methods in functional interface?

A Functional Interface can have Methods of Object Class We can have any number of methods of object classes in our Functional Interface.

What is required for an interface method that has a body quizlet?

What is required for an interface method that has a body? The method header must begin with the key word default.

Which of the following is true about the protected access modifier?

Q 9 - Which of the following is true about protected access modifier? A - Variables, methods and constructors which are declared protected can be accessed by any class.

What is an interface quizlet?

What is an interface? It's a complete abstract class with no implementation at all, it allows the creator to implement method names, argument lists and return types but not method bodies, an interface is contract between the client and.