Chapter 9. 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 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 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 their control) need to be modified to reflect the new interface contract. This is particularly problematic because the Java 8 API introduces many new methods on existing interfaces, such as ...

Get Java 8 in Action: Lambdas, streams, and functional-style programming 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.