SCJP, SCJD, SCWCD, SCBCD, SCEA, SCJP6
To be obtained: SCEA 5
I don't understand the last senstence: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
SCJP, SCJD, SCWCD, SCBCD, SCEA, SCJP6
To be obtained: SCEA 5
Larry Olson wrote:
I don't understand the last senstence: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
Just to clarify this a bit further, all this means is that if the overriding method doesn't really throw a checked exception (that the overridden method throws), it doesn't have to declare so. Here is a made up example:
In the above example, the superMethod() has to declare that it throws an IOException, because it does operations that could throw the IOException. But look at the overriding method below:
As you could clearly see above, the overriding superMethod() in the Sub class DOES NOT declare that it throws an IOException, even though the superMethod() in the Super clas DOES declare that it throws an IOException. This is because the overriding method in the Sub class doesn't do any operations that would result in an IOException, hence it DOES NOT have to declare that it throws an IOException, regardless of the fact that the superMethod() in the Super class declares to throw an IOException.
Does that help?
Ben poon wrote:
BUT IF YOU USE A POLYMORPHIC REFERENCE OF THE SUPERCLASS TO REFERENCE THE SUBCLASS OBJECT'S OVERRIDDEN METHOD YOU WILL BE REQUIRE BY THE COMPILER TO ALSO THROW THE EXCEPTION IN THE SUBLASS'S OVERRIDDEN METHOD RIGHT?
CAN SOMEOE EXPLAIN THE LOGIC OF WHY THIS IS REQUIRED BY THE COMPILER?
THANKS
SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3,OCJP8 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Author of ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- OCPJP Online Training -- Twitter -- How to Ask a Question
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|