inheritance - Can't weakening preconditions and strengthening postconditions also violate Liskov Substitution Principle? -


actual precondition of subtype created combining ( using logical or ) preconditions of base type , preconditions of subtype, makes resulting precondition less restrictive

actual postcondition of subtype created combining ( using logical and ) postconditions of base type , postconditions of subtype, makes resulting postcondition more restrictive

the following examples of strengthening preconditions , weakening postconditions, result violate lsp ( link ):

  1. assume base class works member int. subtype requires int positive. strengthened pre-conditions, , code worked fine before negative ints broken.

  2. likewise, assume same scenario, base class used guarantee member positive after being called. subtype changes behavior allow negative ints. code works on object (and assumes post-condition positive int) broken since post-condition not upheld.

a) why isn't considered violation of lsp when overridden method weakens precondition, since method use parameters not acceptable contracts of base type. such, couldn't claim contract of base type violated , result lsp violated?

b) why isn't considered violation of lsp when overridden method strengthens postcondition, since clients invoking method receive subset of possible results of original method. such, couldn't claim contract of base type violated , result lsp violated?

example:

base class postcondition guarantees return value of method within range 1-10, subtype changes postcondition allow return value within range 2-9. code works on object returned method ( , assumes postcondition within range 1-10 ) broken since postcondition not upheld.

sorry, have logical error in considerations.

base class postcondition guarantees return value of method within range 1-10, subtype changes postcondition allow return value within range 2-9.

since code works within range 1-10 , range 2-9 is actually within range 1-10, strenghtening postcondition should never problem.

same weakening preconditions. allowing subtype accept greater range not breaking base type's behaviour. since behaviour introduced in subtype , precondition subtype's methods.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -