Java 8 has introduced the concept of “functional interfaces” that formalizes this idea. A functional interface specifies only one abstract method. Since functional interfaces specify only one abstract method, they are sometimes known as Single Abstract Method (SAM) type or interface.

Note: Functional interfaces can take generic parameters, as in the Comparator<T> and Callable<T> interfaces in the above examples.

For an interface to be treated as a functional interface, it should have only one abstract method. However, it may have any number of default or static methods defined in it. Let us see an example from the Java library to understand this.

Here is the definition of java.util.function.IntConsumer interface (without annotations and javadoc comments):

Though this interface has two members, andThen method is a default method and only acceptmethod is an abstract method. Hence, IntConsumer interface is a functional interface.

@FunctionalInterface annotation

The Java compiler infers any interface with a single abstract method to be a functional interface. However, you can tag functional interface with @FunctionalInterface annotation to affirm that. It is a recommended practice to provide @FunctionalInterface to functional interfaces because the compiler can give better errors/warnings when you have this annotation.

Here is an example of using @FunctionalInterface that has one abstract method, so it will compile cleanly:

It results in a compiler error “no abstract method found in interface” because it only has a default method provided but does not have any abstract methods.

METHODS FROM OBJECT CLASS IN FUNCTIONAL INTERFACES

According to Java Language Specification (version 8.0), “interfaces do not inherit from Object, but rather implicitly declare many of the same methods as Object.” If you provide an abstract method from Object class in the interface, it still remains a functional interface.

For example, consider the Comparator interface that declares two abstract methods:

This interface is a functional interface though it declares two abstract methods: compare() and equals() methods. How is it a functional interface when it has two abstract methods? Because equals() method signature matches from Object , and the compare() method is the only remaining abstract method, and hence the Comparator interface is a functional interface.

The compiler gives the error: “ EqualsInterface is not a functional interface: no abstract method found in interface EqualsInterface ”. Why? Since the method equals is from Object , it is not considered as a functional interface. Do check out this bookfor more learning about functional interfaces. You can download source code here