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 the sort method on the List interface that you used in previous chapters. Imagine all the angry maintainers of alternative collection frameworks such as Guava and Apache Commons who now need to modify all the classes implementing the List interface to provide an implementation for the sort method too!

9.1. Evolving APIs

 
 

9.2. Default methods in a nutshell

 
 
 

9.3. Usage patterns for default methods

 
 

9.4. Resolution rules

 
 
 

9.5. Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest