The exam watch on the page 108 says in a nutshell that if the overridding method does not declare a exception that is declared in the overridden method the code will not compile.
but I write similar code, and its working as follow:
Sorry Jose Coqueiro but the book is correct and you are wrong.
It looks like you are modifying the code and not following what the exam watch has said exactly, if you look carefully to your main() method it looks like you throws the exception there. So if you remove the Exception in your main() method then the error will be reported by the compiler, that Exception must be caught or thrown again which is what you did exactly therefore you should not have complained why your code works. If you want to test something in the future for confirming a solution, you need to make sure to stick with the conditions.
Joined: Nov 15, 2008
right...one more doubt about it!!!
in the code posted earlier....
when jvm call the line
it should not print "Class1" since the method className in the Class2 does not throw the Exception...and the compiler thinks that Im calling the superclass method?
I think there is a difference between compiler vs runtime on this question.