Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
1  
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
2  
@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
1  
@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
2  
@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

2 Answers 2

up vote 8 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
3  
@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
2  
@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
1  
@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 - http://en.wikipedia.org/wiki/Liskov_substitution_principle

share|improve this answer

Your Answer

 
discard

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.