CS FundamentalsVery hardProduct-company design interview

How many of these subclass changes violate the Liskov Substitution Principle? 1. The overriding method accepts a wider range of inputs than the base method. 2. The overriding method guarantees a stronger result (e.g. a sorted list where the base promised just a list). 3. A ReadOnlyList subclass throws UnsupportedOperationException from add(), which the base List contract allows callers to use. 4. The subclass requires a non-null argument where the base method accepted null.

  1. A. 1
  2. B. 2
  3. C. 3
  4. D. 4

Correct answer: B. 2

Subtypes may weaken preconditions (1) and strengthen postconditions (2). Throwing on an operation the base contract supports (3) and demanding more of callers (4) break code written against the base type, so 2 changes violate LSP.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic