NivaarExam PrepOfficial exam papers ↗

25-Comp-B11 Advanced Software Design · May 2015

Question 11 of 27: 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 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 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 (applied concretely in Question 12).

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 24), 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 its setWidth no longer honours (it weakens) the implicit postcondition that setWidth leaves height unchanged.

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