NivaarExam PrepOfficial exam papers ↗

25-Comp-B11 Advanced Software Design · May 2015

Question 26 of 27: Inheritance vs. Delegation

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

98-Comp-B11 Advanced Software Design — National Exams, May 2015. 3 hours, closed book exam with one aid sheet allowed (written on both sides), no calculator permitted. The paper is organized into five parts, and candidates were instructed to answer any four (4) questions in Part I, any three (3) in Part II, any three (3) in Part III, any two (2) in Part IV, and any four (4) in Part V — only the first questions answered, in each part, as they appear in the answer book are marked. All questions carry equal weight, so the 16 questions actually marked (4+3+3+2+4 of 27) each count for 100/16 ≈ 6.25% of the paper. All 27 questions are answered below for completeness.

Reference texts: Sommerville, Software Engineering (10th ed., Pearson) — software processes, requirements engineering, agile methods, design principles, dependability; 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 (Adapter, Bridge, Strategy, Observer, Template Method, Composite, etc.); Sebesta, Concepts of Programming Languages (12th ed.) — polymorphism, dynamic binding, inheritance and language-level object semantics; Bertrand Meyer, Object-Oriented Software Construction — design by contract, preconditions/postconditions/invariants, the open–closed principle; Barbara Liskov's 1987 substitutability paper for Question 11; Rogers, Sharp & Preece, Interaction Design, and Nielsen, Usability Engineering, for Question 21's HMI-specific non-functional requirements.

PART I — General Principles (answer any 4 of 7)

Question 26: 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) and the Object Adapter variant (Question 14).
  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.