11.1. Introduction¶
In the Interfaces Chapter,
a general approach to implementing an interface was presented that
requires the explicit declaration of a new class, the implements
keyword, and method overrides. The implemented interfaces in that
chapter often included multiple abstract methods that needed to
be overriden. We recommend taking a few minutes to review that
content before proceeding.
In this chapter, we will see an alternative way to implement an interface that contains only one abstract method (called a functional interface) using a lambda expression that requires less code than the class-based approach we saw in chapter 5.
In other words, a lambda expression is a special kind of expression that can be used to implement and instantiate objects of single-method classes using fewer lines of code. More precisely, a lambda expression lets us define a class that implements a functional interface and instantiate that class all using a single expression, sometimes using a single line of code.