World Library  
Flag as Inappropriate
Email this Article

Fragile base class

Article Id: WHEBN0000316681
Reproduction Date:

Title: Fragile base class  
Author: World Heritage Encyclopedia
Language: English
Subject: Fragile binary interface problem, Class (computer programming), FBC
Collection: Class (Computer Programming)
Publisher: World Heritage Encyclopedia
Publication
Date:
 

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]

Contents

  • Java example 1
  • Solutions 2
  • See also 3
  • References 4
  • External links 5

Java example

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 {

  private int counter = 0;

  void inc1() {
    counter++;
  }

  void inc2() {
    counter++;
  }

}

class Sub extends Super {

  @Override
  void inc2() {
    inc1();
  }

}

Calling the dynamically bound method inc2() on an instance of Sub will correctly increase the field counter by one. If however the code of the superclass is changed in the following way:

class Super {

  private int counter = 0;

  void inc1() {
    inc2();
  }

  void inc2() {
    counter++;
  }
}

a call to the dynamically bound method inc2() on an instance of Sub will cause an infinite recursion between itself and the method inc1() of the sub-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.

Solutions

  • Objective-C has categories as well as non-fragile instance variables.
  • Component Pascal deprecates superclass calls.
  • 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 (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.
  • Julia allows only subtyping of abstract types and uses composition as an alternative to inheritance. It however has multiple dispatch.

See also

References

  1. ^ "Selective Open Recursion: A Solution to the Fragile Base Class Problem", Jonathan Aldrich
  2. ^ "Selective Open Recursion: A Solution to the Fragile Base Class Problem", Lambda the Ultimate

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
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 



Copyright © World Library Foundation. All rights reserved. eBooks from Cloud eBook Library are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.