Inheritance is a broad subject and there is a great deal of variation in precisely how various languages deal with it. Szyperski in ComponentSoftware identifies three uses for inheritance:
- subclassing: reusing code via implementation inheritance
- subtyping: the inheritance of interfaces
- a promise of substitutability: ie polymorphism
Few people would argue against subtyping or polymorphism, but subclassing is much more controversial. The fundamental problem is that subclassing breaks encapsulation. This has a variety of evil effects:
- It encourages clients to tweak the behavior of base classes in ways that the original developer may not have envisioned. This often requires an intimate knowledge of the base class implementation and means that changes to the base class can easily break the subclass (FragileBaseClassProblem). This is an especially serious problem for component oriented development.
- Multiple inheritance is a tricky business when implementation inheritance is allowed. The worst problem is how to deal with diamond shaped inheritance graphs. Every language seems to have its own rules for handling MI, but they all seem rather complex and prone to surprises. (See MultipleInheritanceIsNotEvil for a contrasting view).
- Inheritance is a clumsy and overly restrictive way to assemble objects. It binds the composition of objects extremely early which limits flexibility and it's usually difficult to change the behavior of existing types. For example, most languages won't let you change the behavior of the standard string class.
What is needed I think is a paradigm shift in language design: away from implementation inheritance and to a model based on composition or aggregation. The
ScarletLanguage is attempting to do this. Scarlet objects are composed of multiple implementations that are treated as black boxes. This gives developers most of the benefits of single and multiple inheritance, but in a safer and more controlled way. It also allows for a high degree of dynamism (so plugins for example can modify existing object layouts).
--JesseJones
CategoryLanguage, CategoryLanguageFeature, CategoryEvil