This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
on line 20 you rethrow the Throwable t which IS-A IllegalMonitorStateException. You upcast it to RuntimeException before rethrowing. Nevertheless, this does not affect the fact, that t still IS-A IllegalMonitorStateException. Therefore, on line 25, the proper catch is invoked.
This concept is the same as with regular objects. For example, you can write
when we are using polymorphism for objects, the object type will never change, just the reference type will change when we create new instance of the class, then due to start() method, object of IllegalMonitorStateException will be created and thrown there
now in the catch blocks we are just referring to the object of type IllegalMonitorStateException using the super type
This doesn't work in the same way as castig primitives (char to byte, byte to int, int to long etc.).
If an object of given class is created, it will stay an object of this class forever - it is not possible to change the class of any object.
You cannot "upcast" the object - it is not possible.
Look at this simple example:
As You see - "upcasting" doesn't work, the class of the object does not change.
I would just like to make a personal comment about the example Saibabaa gave. I haven't come across it on ExamLab, but I hope I don't, it has to be one of the most convoluted examples I have experienced, it took me 15 mins to figure out what was going on! Perhaps its just my slow mind Yet goodness, its a humdinger of a poorly written program (in my opinion of course )
be a well encapsulated person, don't expose your privates, unless you public void getWife()!