Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: default method

...

Code Block
bgColor#ccccff
abstract class Client implements User  {
  public abstract void freeService(); 
  // Delegate implementation of new method to subclasses
  // Other concrete implementations
}

Compliant Solution (Java 8 Default Method)

Java versions 8 and newer allow an interface to provide a default method, which allows for extending interfaces without forcing the modification of preexisting classes which implement the interface.  Classes that implement this interface can ignore freeService() in which case the default implementation is used, or they can reimplement freeService() themselves, or they can declare it to be abstract, re-establishing the requirement for subclasses to to provide an implementation.

Code Block
bgColor#ccccff
public interface User {
  boolean authenticate(String username, char[] password);
  void subscribe(int noOfDays);
  default void freeService() {   // Introduced after the class is publicly released
    // ...
  }
}

Applicability

Failing to publish stable, flaw-free interfaces can break the contracts of the implementing classes, pollute the client API, and possibly introduce security weaknesses in the implementing classes.

Bibliography

[Bloch 2008]Item 18, "Prefer Interfaces to Abstract Classes"

...


...