Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I read that Liskov substitution principle is violated if :

  1. Pre conditions are strengthened .

  2. Post conditions are eased out.

But I don't get fully yet how these two points would violate Liskov Substitution principle . Can some one please explain with an example. Specifically how would any one of the above conditions cause a situation where a subclass object can not be substituted for a superclass object ?

share|improve this question
That's kind of like asking why the rules of chess are violated if you move a knight in a straight line. – pdr Feb 19 '13 at 18:56
@pdr through this question I want to better understand the formulatin of LSP . – Geek Feb 19 '13 at 18:58
@pdr: I'd say it's more like asking: Why can't knights move in a straight line? – Joel Etherton Feb 19 '13 at 18:59
@JoelEtherton: Not really. Cause the answer to that would be "because it says so in the rules." That is the only reason. This question is circular, like mine about the knight. It wouldn't be chess / LSP if the rules were different. – pdr Feb 19 '13 at 19:02
@pdr: That analogy is flawed. The rules of chess are invented arbitrarily. The rules of the LSP are a logical consequence of the goal of type-safety. They can be logically derived, and counterexamples where not following breaks type-safety can be found. – Jörg W Mittag Feb 19 '13 at 19:46
up vote 11 down vote accepted
  1. Assume your baseclass works with a member int. Now your subtype requires that int to be positive. This is strengthened pre-conditions, and now any code that worked perfectly fine before with negative ints is broken.

  2. Likewise, assume the same scenario, but the base class used to guarantee that the member would be positive after being called. Then the subtype changes the behavior to allow negative ints. Code that works on the object (and assumes that the post-condition is a positive int) is now broken since the post-condition is not upheld.

These are of course trivial examples, but the concept holds. Stuff like leaving a file/database connection open is an example of an eased post-condition that leads to issues.

share|improve this answer
How is your example in the second paragraph a case of weakening of post conditions ? – Geek Feb 19 '13 at 19:07
@Geek The result being a positive integer is "stronger" than just being an integer. "Stronger" here means something similar to inclusion for sets. So when you return any integer, your postcondition is "weaker" than (i.e. "it includes") returning the positive integers. – Andres F. Feb 19 '13 at 19:09
@Geek No, I'm talking about the second paragraph. The stronger postcondition of "just positive integers" (let's call it A) gets changed to the weaker postcondition of "all integers" (B). A is weaker than B because it contains it. – Andres F. Feb 19 '13 at 19:13
@AndresF. ok I see it now.thanks .I wish I could give you a +1 for the explanatory comment. – Geek Feb 19 '13 at 19:14

This example is pretty much beaten to death, but consider the Square/Rectangle or Circle/Ellipse possibility. Suppose you have a base class Rectangle that defines an object with a length and width. If you have a Square class that inherits the Rectangle class, it would have a rule in its setter/getter that would require that any change to length or width would alter its counterpart. These dimensional requirements strengthen pre-conditions because a rectangle substituted for a square would be missing these dimensional requirements. Suppose you reverse the inheritance so that a Rectangle inherits a Square, you would be weakening post conditions by relaxing the dimensional requirements to allow the Rectangle to behave independently.

However, if you were to remove the dimensional change capability, the substitution principle holds because if neither a Rectangle nor a Square may change dimensions, then they have equal pre and post conditions regardless of inheritance. Both have a length, both have a width, and neither can alter those values.

ref: Wikipedia -

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.