Examples of Liskov Substitution

Posted by james lewis on Programmers See other posts from Programmers or by james lewis
Published on 2012-11-21T08:52:11Z Indexed on 2012/11/21 11:22 UTC
Read the original article Hit count: 320

I'm facilitating a session next week on the Liskov Substitution Principle and I was wondering if anyone had any examples of violations 'from the trenches'? I'm looking for something other than uncle Bob's rectangle - square problem and the persistent set problem he talks about in A-PPP (although that is a great example).

So far I'm using the example of a (very simple) List and an IndexedList as the 'correct' use of inheritance. And the addition of a Set to this hierarchy as a violation (as a Set is distinct; strengthening the pre condition of the Add method).

I've also taken this great example and it's solution from this question

Both those examples are great but I'm looking for something more subtle and harder to spot. So far I've come up with nothing so if you've got a great, subtle example post it up. Also, any metaphors you've come across that helped you understand LSP would be really useful too.

© Programmers or respective owner

Related posts about object-oriented-design

Related posts about solid