Composition and inheritance

Steven Lowe has an article on the ThoughtWorks blog, elaborating on Composition vs. Inheritance: How to Choose? The text covers examples of how to misuse inheritance.

Composition

The purpose of composition is obvious: make wholes out of parts.

Steven Lowe

In my words:Use composition for has-a relationships.

Inheritance

Inheritance captures semantics (meaning) in a classification hierarchy (a taxonomy), arranging concepts from generalized to specialized. […] This makes the subclass more tightly coupled to its superclass than it would be if it merely used an instance of the superclass as a component instead of inheriting from it. […] Inheritance captures mechanics by encoding the representation of the data (fields) and behavior (methods) of a class and making it available for reuse and augmentation in subclasses. Mechanically, the subclass will inherit the implementation of the superclass and thus also its interface.

Steven Lowe

In my words:Use inheritance for is-a relationships. By Steven´s guidance, inheritance should only be used when:

Comments