11.2. Functional Interfaces

11.2.1. What is a Functional Interface?

Since Lambda Expressions are used to implement functional interfaces, it is important to first fully understand functional interfaces.

In Java, a functional interface is an interface that has just one abstract method aside from the methods of the Object class, and thus represents a single function contract [FUNCTIONAL]. In other words, they are a guarantee that implementing classes contain an implementation of that one abstract method.

11.2.2. Implementing an Interface

Before we discuss any new syntax for implementing functional interfaces, it is important to note that a functional interface is still an interface and can be implemented using the same exact approach we learned earlier this semester. The five-step process described below summarizes that traditional approach to implementing an interface in Java:

  1. Sign the contract: Create a class in its own .java file and include the appropriate implements clause in the class declaration.

  2. Meet the minimum requirements: Override the abstract methods from the interface so that the code compiles.

  3. Meet the full requirements: Ensure that each overridden method aligns with the expectations outlined in the interface documentation.

  4. Instantiate: Create one or more objects of the class.

  5. Interact using the interface: When interacting with the objects, use a variable with the interface as its type so that your code works with any object (of a class) that implements the interface and not just objects of the new class you created.

The code described in step 5 may have been written before step 1 is started, especially in cases where other classes that implement the interface already exist. The overall process outlined above has real, tangible benefits. For example, the new class can:

  1. be reused (by utilizing its constructor);

  2. have instance variables that allow its objects to manage their state;

  3. include helper methods; and

  4. include documentation that differs from or adds on to the interface documentation in some way.

All of that sounds great, but what if you do not need one or more of those benefits? What if you don’t need any of them? Suddenly, the five-step process described earlier for implementing the interface sounds tedious, especially in cases where the interface only has one abstract method.

11.2.3. Implementing a Functional Interface

If an interface in Java is considered a functional interface, then an alternative two-step process can be used to implement and utilize it using Java’s lambda expression syntax:

  1. Instantiate: Use a lambda expression to create an object of an unnamed class that implements the interface by defining what the override behavior should be for the one abstract method in the interface (all in one place).

  2. Interact using the interface: When interacting with the object, use a variable with the interface as its type so that your code works with any object (of a class) that implements the interface and not just objects of the new class you created.

Test Yourself!

  • Is a functional interface allowed to have more than one abstract method?

  • Is Predicate a functional interface? Why or why not?

  • Is Comparator a functional interface? Why or why not?

  • Is Runnable a functional interface? Why or why not?

  • Is TemporalAccessor a functional interface? Why or why not?

Test Yourself Solutions (open after answering the questions above)
  • Yes, if the additional abstract methods are also found in the Object class.

  • Yes! While the interface contains several default and static methods, it has only one abstract method called test.

  • Yes! The interface has two abstract methods, compare and equals. However, the equals method is also found in Object.

  • Yes! Runnable has a single abstract method called run.

  • No, the interface has two abstract methods and neither are found in Object.