In the Contract project, you specify a set of preconditions and a set of post-conditions for the function. For example, let's say you write a memory allocation function. You need it to accept a positive integer as input, and output a uniformly aligned pointer as the result.
The weakening of the precondition means that when creating a derived class, it must accept any input that the base class can accept, but it can accept other inputs. Using the above example, a derived class can be written to accept a non-negative integer instead of positive integers.
On the side of the result, you must make sure that the result from the derived function satisfies all the requirements for the base function, but can also add additional restrictions. For example, a derivative version of the above function can only decide to get results that are multiples of 8. Each multiple of 8 is clearly even, so it still meets the requirement of the underlying function, but also imposes an additional restriction.
The opposite action does not work: if the base class function allows you to enter non-negative integers, then the derived class must continue to accept all non-negative integers as input. Attempting to change it to accept only positive integers (i.e., Deviation 0, which is allowed by the base class) will not be allowed - your derived class can no longer be replaced by the base version under any circumstances.
Similarly with the results: if the base class imposes a βmultiple 8β requirement on the result, the derived version must also ensure that all results are a multiple of 8. Returning 2 or 4 will violate this requirement.
source share