Class inheritance defines a software class using another software class's implementation and may add or override behavior. When it also establishes subtyping, the derived software class must satisfy the base software class's behavioral contract for clients to substitute it safely. Code reuse alone is not sufficient justification for an inheritance relationship. Object composition often better represents a software object containing a replaceable component.
A subclass may depend on its superclass's internal calls to overridable object methods. A base implementation change that preserves the base abstraction can then change subclass behavior. For example, a set subclass may count insertion attempts by overriding insertion. If a revised deletion operation rebuilds the set by dynamically dispatched clear and insert calls, it unexpectedly resets and increases that counter. Object composition can isolate the counting wrapper from the component's internal dispatch, provided only wrapper operations can mutate the owned component.
Articles by others on the same topic
There are currently no matching articles.