Chapter 13. Default methods

This chapter covers

  • What default methods are
  • Evolving APIs in a compatible way
  • Usage patterns for default methods
  • Resolution rules

Traditionally, a Java interface groups related methods together into a contract. Any (nonabstract) class that implements an interface must provide an implementation for each method defined by the interface or inherit the implementation from a superclass. But this requirement causes a problem when library designers need to update an interface to add a new method. Indeed, existing concrete classes (which may not be under the interface designers’ control) need to be modified to reflect the new interface contract. This situation is particularly problematic because the Java 8 API introduces ...

Get Modern Java in Action now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.