Fragile base class
The fragile base class problem is a fundamental architectural problem of object-oriented programming systems where base classes (superclasses) are considered "fragile" because seemingly safe modifications to a base class, when inherited by the derived classes, may cause the derived classes to malfunction. The programmer cannot determine whether a base class change is safe simply by examining in isolation the methods of the base class.
One possible solution is to make instance variables private to their defining class and force subclasses to use accessors to modify superclass states. A language could also make it so that subclasses can control which inherited methods are exposed publicly. These changes prevent subclasses from relying on implementation details of superclasses and allow subclasses to expose only those superclass methods that are applicable to themselves.
Another alternative solution could be to have an interface instead of superclass.
The fragile base class problem has been blamed on open recursion (dynamic dispatch of methods on this
), with the suggestion that invoking methods on this
default to closed recursion (static dispatch, early binding) rather than open recursion (dynamic dispatch, late binding), only using open recursion when it is specifically requested; external calls (not using this
) would be dynamically dispatched as usual.[1][2]
Solutions
- Objective-C has categories as well as non-fragile instance variables.
- Component Pascal deprecates superclass calls.
- Java and D allow inheritance or overriding a class method to be prohibited by labeling a declaration of a class or method, respectively, with the keyword "final". In the book Effective Java, author Joshua Bloch writes (in item 17) that programmers should "Design and document for inheritance or else prohibit it".
- C# and VB.NET like Java have "sealed" and "Not Inheritable" class declaration keywords to prohibit inheritance.
- Scala require a subclass to use keyword "override" explicitly in order to override a parent class method. In the book "Programming in Scala, 2nd Edition", the author writes that (with modifications here) If there was no method f(), the client’s original implementation of method f() could not have had an override modifier. Once you add the f() method to the second version of your library class, a recompile of the client code would give an compile error instead of wrong behavior.
See also
- Fragile binary interface problem
- Implementation inheritance
- Inheritance semantics
- Software brittleness
- Virtual Inheritance (object-oriented programming)
References
External links
- Leonid Mikhajlov and Emil Sekerinski: A Study of The Fragile Base Class Problem. ECOOP 1998: 355-382. - Scientific description and analysis of the fragile base class problem.
- Why Extends is Evil - An example in Java