Java - Lambda Expressions Functional Interfaces

Introduction

A functional interface is an interface with exactly one abstract method.

The following types of methods in an interface do not count for defining a functional interface:

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

An interface with more than one abstract method can still be a functional interface if all but one of them is a redeclaration of the methods in the Object class.

For example

package java.util;

@FunctionalInterface
public interface Comparator<T> {
        // An abstract method declared in the interface
        int compare(T o1, T o2);

        // Re-declaration of the equals() method in the Object class
        boolean equals(Object obj);

        /* Many static and default methods that are not shown here. */
}

The Comparator interface contains two abstract methods: compare() and equals().

The equals() method in the Comparator interface is a redeclaration of the equals() method of the Object class.

The equals() method does not count against the one abstract method requirement for it to be a functional interface.

Related Topics