• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Overriding problem

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

Thanks for your answers. It is very helpful

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 6833
1299
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic