For example, a pre-condition for a pop method in a stack class might express that the method assumes the stack will not be empty. Post-conditions are the other side of the coin. A post-condition for a piece of code describes the effect of the code on the program state. It therefore forms a goal for the programmer of that code and is a guarantee that can be relied upon by its users. For example, a stack push method might have a post-condition which states that the number of items on the stack after push is called is one more than the number of items on the stack before the call.
Class invariants are invariants on the methods of a class.
A class invariant can be assumed to be true before any method of the class executes. Also, the execution of any method of the class must maintain the invariant. That is, the invariant must also be true after the method has finished execution. For example, a stack class might have an invariant that the number of items in the stack is always greater than or equal to zero. Writing formal specifications can aid the correct development of a program.