Liskov Substitution Principle

From Notes
Jump to navigation Jump to search
Subclasses should be substitutable for their base classes


Precise Definition

Let q(x) be a property provable about objects x of type T. Then q(y) should be provable for objects y of type S, where S is a subtype of T

class Base { ... };
class Derived : public Base { ... };
void q(Base& b); // should work, even if b is an instance of Derived


Design by Contract

Method in derived class must

  • have preconditions that are no stronger than those of the base's method it is overriding
  • have postconditions no weaker than those of the base's method it is overriding

In other words

Derived methods should expect no more and provide no less than parent method.</blockquote