The fragile base class problem is a fundamental architectural problem of object-oriented programming systems where base classes 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, with the suggestion that invoking methods on this default to closed recursion rather than open recursion, only using open recursion when it is specifically requested; external calls would be dynamically dispatched as usual.
The following trivial example is written in the Java programming language and shows how a seemingly safe modification of a base class can cause an inheriting subclass to malfunction by entering an infinite recursion which will result in a stack overflow. class Super class Sub extends Super
Calling the dynamically bound method inc2 on an instance of Sub will correctly increase the fieldcounter by one. If however the code of the superclass is changed in the following way: class Super
a call to the dynamically bound method inc2 on an instance of Sub will cause an infinite recursion between itself and the methodinc1 of the super-class and eventually cause a stack overflow. This problem could have been avoided, by declaring the methods in the superclass as final, which would make it impossible for a sub-class to override them. However, this is not always desirable or possible. Therefore, it is good practice for super-classes to avoid changing calls to dynamically-bound methods.
Java, C++ 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 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, and require a subclass to use keyword "override" on overriding methods, the same solution later adopted by Scala.
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 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.