NivaarExam PrepOfficial exam papers ↗

25-Comp-B11 Advanced Software Design · May 2014

Question 11 of 25: The Liskov Substitution Principle

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

Notes on this paper

98-Comp-B11 Advanced Software Design — National Exams, May 2014. 3 hours, open book, no calculator permitted. The paper is organized into five parts, and candidates were instructed to answer any three (3) questions in Part I, any four (4) in Part II, any three (3) in Part III, any one (1) in Part IV, and any one (1) 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 12 questions actually marked (3+4+3+1+1 of 25) each count for 100/12 ≈ 8.3% of the paper. All 25 questions are answered below for completeness.

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 (answer any 3 of 5)

Question 11: The Liskov Substitution Principle (Part II)

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.

The Liskov Substitution Principle (LSP, Barbara Liskov, 1987) states that objects of a subtype must be substitutable for objects of its supertype without altering the correctness of a program: anywhere client code holds a reference typed as the supertype, substituting a subtype instance must not break any guarantee that client code relies on.

Formalized in Design-by-Contract terms (Question 10), a subtype's precondition must be no STRONGER than the supertype's (the subtype cannot demand more of callers than the supertype promised to require), and its postcondition must be no WEAKER (the subtype must still deliver at least what the supertype's contract promised) — informally, "contravariant preconditions, covariant postconditions," with invariants preserved as well.

Why it is useful. LSP is the formal underpinning of safe inheritance-based polymorphism: without it, substituting a subtype for its declared supertype silently breaks client code that was written once, correctly, against the supertype's documented contract — defeating the entire purpose of subtype polymorphism (Question 7), which is that client code need not even know which concrete subtype it holds. LSP gives a concrete, checkable test for "is this a valid is-a relationship for INHERITANCE purposes," beyond an informal, real-world "is-a" intuition that can be misleading.

Classic violation. A class Square that extends Rectangle, overriding setWidth/setHeight so both sides stay equal, breaks client code such as r.setWidth(5); r.setHeight(4); assert r.getArea() == 20; — the assertion, valid for any genuine Rectangle, fails for a Square because setting the height silently also changed the width. A square "is-a" rectangle in the everyday geometric sense, but Square is NOT a valid Rectangle SUBTYPE under LSP, because it weakens (drops) the implicit postcondition that setWidth leaves height unchanged.

The practical consequence guides Question 23's inheritance-vs-delegation choice directly: when a natural is-a relationship cannot preserve the supertype's contract, prefer composition/delegation over inheritance.