You will need a compiler that supports checking your invariants; possibly an extensible compiler such as JastAddJ .
Most projects rely on either unit tests to verify behavior or static analysis after compilation using tools such as findbugs or Coverity .
It is possible that in a language such as Scala, you can encode the desired invariants in the type system, in which case the compiler will be able to verify that a particular subclass obeys the contract.
source share