Skip to main content
InterviewHubPRO
Section Overview
Q38

Question 38 of 50

Part 3: Enterprise Architecture, Package Design & Enforcement (25 - 35)

76%
SOLID & OOP Principles (50 Qs)•Part 3: Enterprise Architecture, Package Design & Enforcement (25 - 35)

Liskov Substitution Principle (LSP) Contracts: Preconditions, Postconditions, and Invariants.

senior LevelSystem Design

In Simple Words: Formulated by Barbara Liskov: Subtypes must be substitutable for their base types without altering program correctness.

  • Preconditions cannot be strengthened: If base method process(amount) accepts any positive number ($> 0$), a subclass cannot demand amount >= 100.
  • Postconditions cannot be weakened: If base method guarantees return value != null, a subclass cannot return null.
  • Invariants must be preserved: Any internal consistency rule held by the superclass must continue to hold in the subclass.
  • Exception Rule: A subclass cannot throw new checked exceptions that the caller does not expect from the base type contract.