Liskov Substitution Principle

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


Precise Definition

Let be a property provable about objects of type . Then should be provable for objects of type , where is a subtype of

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