Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding problem

 
George Fung
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

Thanks for your answers. It is very helpful

 
Ben poon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4511
271
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic