• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assertions  RSS feed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ALL,
Please tell me what is assertion and where we use assertion and how it is related to inheritence.

Thanks & Regards,
Vivek Mishra
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assertions come from the computer science idea of invariants. If you know that at a certain point, g should be non-negative, you can write
assert g >= 0 : "g went negative";
If g is < 0 at that point, you will get an exception labeled with the message you put in the assert statement.

Other recommended uses of asserts are places in the program you should never reach,
default: assert false : "we got a bad switch expression value";
and checking calling argument to non-public methods.
assert grossPay >= 0 : "Tax Calculator called with negative wages";

Using assert requires specific options in both the javac and the java commands. You can leave debugging statements in your code when running production with no performance penalty just by not enabling them in the java command.

I don't know how assertions are related to inheritance.
[ December 25, 2004: Message edited by: Mike Gershman ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!