Nivaar Exam Prep

25-Comp-B11 Advanced Software Design · May 2014

Question 23 of 25: 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). 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.

PART I — General Principles

Question 23: Inheritance vs. Delegation (Part V)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

(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:

  1. 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.
  2. 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.
  3. 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).
  4. 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.
Stack List is-a (leaks insertAt/removeAt) Stack − list: List List has-a / delegates (push/pop only)
Left: inheritance leaks List's full interface into Stack. Right: delegation exposes only push/pop/peek, forwarding internally to a private List.