25-Comp-B11 Advanced Software Design · December 2014
Question 24 of 26: Inheritance vs. Delegation
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
Notes on this paper
Reference texts: Sommerville, Software Engineering (10th ed., Pearson) — software processes, requirements engineering, agile methods, design principles; Pressman, Software Engineering: A Practitioner's Approach (9th ed.) — supplementary process and quality coverage; Gamma, Helm, Johnson & Vlissides (GoF), Design Patterns: Elements of Reusable Object-Oriented Software — structural/behavioural pattern catalogue (Proxy, Bridge, Strategy, Observer, Template Method, Composite, etc.); Sebesta, Concepts of Programming Languages (12th ed.) — polymorphism, dynamic binding, inheritance and language-level object semantics (also underpins the Java/C++ discussion in Part V); Brown, Malveau, McCormick & Mowbray, AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis — anti-pattern catalogue (Question 18). Bertrand Meyer's Object-Oriented Software Construction is cited by name where the paper's own vocabulary (design by contract, open–closed principle) originates there; Barbara Liskov's 1987 substitutability paper is likewise cited by name for Question 11.
(Implementation) inheritance creates a static, compile-time is-a relationship in which the subclass automatically receives — and can be affected by — the ENTIRE public and protected interface of its superclass, including operations irrelevant or even harmful to the subclass's own abstraction. This is exactly the "message leakage" problem: a Stack implemented by extending List inherits insertAt(index)/removeAt(index), letting client code bypass the Stack's push/pop discipline entirely and violate its own invariant.
Delegation (composition plus forwarding) creates a runtime has-a relationship: the using class holds a private reference to an instance of the used class and explicitly forwards only the specific operations it chooses to expose through its own interface — the used object's full interface never leaks through.
When delegation is necessary in place of inheritance:
When the is-a relationship doesn't actually hold in the LSP sense (Question 11) — the subclass cannot honestly satisfy the superclass's full contract without weakening it.
When only a PARTIAL subset of the superclass's interface is wanted or appropriate — the Stack-via-List message-leakage case above; delegation lets the class expose only push()/pop()/peek(), hiding insertAt/removeAt entirely.
When the relationship needs to change at RUNTIME — which object is delegated to can be swapped after construction, while inheritance's is-a binding is fixed at compile time; this is precisely the mechanism behind the Strategy pattern (Question 15).
When avoiding tight coupling to a superclass's IMPLEMENTATION details across a fragile-base-class boundary — a change to the superclass's internals can silently break subclasses that rely on its exact behaviour in ways delegation's narrower forwarding interface never exposes.
Left: inheritance leaks List's full interface into Stack. Right: delegation exposes only push/pop/peek, forwarding internally to a private List.