• Post Reply Bookmark Topic Watch Topic
  • New Topic

Liskov Substitution Principle: Weakening the Preconditions  RSS feed

 
Brian Campbell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

Apologies in advance if this is the wrong forum, but it seems to be as good a candidate as any. I'm taking a Java-based software engineering course in graduate school, and the students have had quite an energetic offline discussion over one aspect of the Liskov substitution principle.

We all understand the main point: if S is a subtype of T, objects of subtype S may be substituted for objects of type T without altering any of the desirable properties of the program.

We are in fact using Liskov's Program Development in Java: Abstraction, Specification, and Object-Oriented Design as one of our textbooks, and as many of you know, she goes on to say in that book that a subtype can weaken but not strengthen the preconditions of the supertype, and can strengthen but not weaken the postconditions.

One slide generated quite a bit of discussion during one of the classes and via email afterwards (the professor has stepped back for the moment and let us duke it out).

Here's the question:

Class SuperType
//REQUIRES: x > 5

Class SubTypeA
//REQUIRES: x > 6

Class SubTypeB
// REQUIRES: x > 4

x>5 --> x>4?
x>5 --> x>6?

Which is the *weaker* precondition?

I can share quite a bit of discussion and conclusions here, but I would prefer to let some wise heads chime in first.

Thanks!
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider a situation where a some code was written earlier keeping the Superclass precondition- x > 5 in mind. So if we try to introduce a SubType then any existing code need not be changed and hence the precondition has to be weaker than the SuperType's. And the weaker in this case would be x > 4, because if you consider x > 6 then older code which used x = 6 would break and hence its a Stronger precondition. But if you consider x > 4 then all the SuperTypes pre conditions are met.

Summarising I think x > 4 is a weaker precondition to x > 5.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about the set of values that meet the pre-condition. For the condition to be weaker, that set has to be bigger (strictly, it also has to contain the set of values that meet the previous condition), because it has to be easier to meet that condition.

Which means I agree with Mohamed.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Thing about the set of values that meet the pre-condition. For the condition to be weaker, that set has to be bigger (strictly, it also has to contain the set of values that meet the previous condition), because it has to be easier to meet that condition.

Which means I agree with Mohamed.


Also in agreement.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
Summarising I think x > 4 is a weaker precondition to x > 5.


Right.

"A subclass can weaken but not strengthen the preconditions" means that the subclass is allowed to be more permissive than the superclass but not more restrictive. In other words, it has to accept everything the superclass would accept, but it is also allowed to accept more.

X > 4 is weaker (more permissive) than X > 5 because it includes everything covered by X > 5, plus things that are not covered by X > 5.
 
Brian Campbell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, everybody. That's basically the conclusion that we reached, but it took us a while to get there. :-)

What sealed the deal for the holdouts was a variation of Jeff's response: Imagine that we're only talking about non-negative integers. The range of illegal values in the supertype is {0,1,2,3,4,5}.

The range of illegal values for subtype A is {0,1,2,3,4,5,6}.

The range of illegal values for subtype B is {0,1,2,3,4}.

A smaller range of prohibited values represents a weakened precondition. But it was a fun discussion before things settled down. :-)

Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!