SE450: Choosing Subclassing [35/35] Previous pageContents

GoF: When a subclass overrides some but not all operations, it can affect the operations it inherits as well, assuming they call the overridden operations.

Delegation is difficult to code when strategies are interdependent.

For an example see Painting in AWT and Swing.

paint and update are related hook methods.

repaint is a non-final template method, which (indirectly) calls update.

public abstract class Component ... { ...
  public void paint(Graphics g) {  } // Used for system-triggered painting
  public void update(Graphics g) {   // Used for application-triggered painting
    paint(g); // default implementation
  }
  public void repaint() {
    // requests that the GUI thread call back to "update" at some point
  }
}

By default, overriding paint affects repaint and update.

repaint and update can also be independently overridden.

In my opinion, paint should be abstract.

Previous pageContents