The design for contract concepts gets a little complicated when they are adapted to object-oriented languages.
A class invariant is a property guaranteed by each instance of a class when a method is called (for example, a general precondition for all methods), and that as a result, each method and constructor must ensure that they remain valid when they stop (as a general post-condition) .
They are good for expressing conditions of consistency. The Wallet class, which models the actual wallet, may have a class invariant that the contained amount is always positive.
Class invariants, like the rest of the contract, are inherited. New implementations of the methods should provide the same guarantees as the methods that they replace.
source share