In “Design Patterns”, the Gang of Four recommend two important principles of object oriented design:
- Program to an interface, not an implementation.
- Favor object composition over class inheritance.
Why is the seminal work on Object Oriented design so distinctly anti-inheritance? Because inheritance causes several problems:
- Tight coupling. Inheritance is the tightest coupling available in OO design. Descendant classes have an intimate knowledge of their ancestor classes.
- Inflexible hierarchies. Single parent hierarchies are rarely capable of describing all possible use cases. Eventually, all hierarchies are “wrong” for new uses—a problem that necessitates code duplication.
- Complicated multiple inheritance. It’s often desirable to inherit from more than one parent. That process is inordinately complex and its implementation is inconsistent with the process for single inheritance, which makes it harder to read and understand.
- Brittle architecture. Because of tight coupling, it’s often difficult to refactor a class with the “wrong” design, because much existing functionality depends on the existing design.
- The Gorilla / Banana problem. Often there are parts of the parent that you don’t want to inherit. Subclassing allows you to override properties from the parent, but it doesn’t allow you to select which properties you want to inherit.
Delegation / Differential Inheritance
Cloning / Concatenative Inheritance / Mixins
Closure Prototypes / Functional Inheritance
Complex Object Composition