wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding problem" Watch "Overriding problem" New topic
Author

Overriding problem

George Fung
Ranch Hand

Joined: Jun 12, 2003
Posts: 98
The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.

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.

Can anyone quote a example of it?

Thank you


SCJP, SCJD, SCWCD, SCBCD, SCEA, SCJP6
To be obtained: SCEA 5
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

George, the last line means that, a method which is being overridden by the sub-class can throw an exception which is narrower then
the one Parent class is throwing but it can't throw which is un-related in simple words. For example


If you code above, the first one is legal as FileNotFoundException is child of IOException, but the other one is not legal as it is throwing a broader
exception. Replace it anyother sibling in the heirarchy, and it will cause the same result.

Hope this helps,

Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
And the last line means that, the method that is being overridden by the sub-class should never throw any exception
which it's parent is not throwing. Like for example as i quoted above, IOException is being thrown by the parent class
method. So far so good, child class overrides the method and throws FileNotFoundException. Even that is good as it is
sub-class of IOException. If we say something like this in child


Why this method should throw a ParseException which it's parent class will never throw only. So compiler will stop you there
and will give a compile time error.

Hope this helps,
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
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?
George Fung
Ranch Hand

Joined: Jun 12, 2003
Posts: 98
Dear all,

Thanks for your answers. It is very helpful

Ben poon
Greenhorn

Joined: Apr 22, 2010
Posts: 1
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?





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



Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

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





If you use code like


because you are still using a Super Class Reference. Compiler thinks that you are calling super.superMethod()
so you either have to handle it or declare it!



hope this is clear!


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3057
    
  38

Ben poon, welcome to JavaRanch.
Please understand that using ALL-CAPS letters makes it's hard to read your post. It's also considered as shouting. Have a look at KeepItDown faq.
Please edit your post by clicking the button, and remove those ALL-CAPS letters..


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overriding problem