How to verify the Liskov substitution principle in an inheritance hierarchy?

Posted by Songo on Programmers See other posts from Programmers or by Songo
Published on 2012-10-17T10:45:13Z Indexed on 2012/10/17 11:20 UTC
Read the original article Hit count: 227

Filed under:
|
|
|

Inspired by this answer:

Liskov Substitution Principle requires that

  • Preconditions cannot be strengthened in a subtype.
  • Postconditions cannot be weakened in a subtype.
  • Invariants of the supertype must be preserved in a subtype.
  • History constraint (the "history rule"). Objects are regarded as being modifiable only through their methods (encapsulation). Since subtypes may introduce methods that are not present in the supertype, the introduction of these methods may allow state changes in the subtype that are not permissible in the supertype. The history constraint prohibits this.

I was hoping if someone would post a class hierarchy that violates these 4 points and how to solve them accordingly.
I'm looking for an elaborate explanation for educational purposes on how to identify each of the 4 points in the hierarchy and the best way to fix it.

Note:
I was hoping to post a code sample for people to work on, but the question itself is about how to identify the faulty hierarchies :)

© Programmers or respective owner

Related posts about design

Related posts about learning