Fragile Base Class Problem

Samiul • •

In OOP, the “Fragile Base Class Problem” is where modifying the base class can potentially break the sub-class.

Imagine a base class with two public methods A and B. Is it safe to refactor such that A now calls B? There is no way to know. A sub-class could have already overridden B to call A and the refactoring will result in an infinite recursion in the sub-class.

The fact that the base class has to worry about how a sub-class could modify its non-final method is the fragileness of the “Fragile Base Class Problem”. The problem is only related to non-final methods. Final methods cannot be overridden so the base class can always rely on its implementation.

Some ways to mitigate:

  1. Don’t allow inheritance. Make the class final. If there is no sub-class, there is no risk of breakage.
  2. If inheritance is allowed, avoid coupling between non-final methods. Treat every non-final method as a potential hazard.
  3. Document it properly to guide users on creating a sub-class for the base class safely.