• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception in overridden

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi 'LL
I wanted to know why an overriding method cannot throw any checked exception other than the same excpetion or subclass of an exception thrown by the overrridden method??
why java made it necessary..
thank you
rishi
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Greetings all. I give my thoughts on this:
Overriding is a form of polymorphism. We all know when we override a method,
the same method exists in its super class with probably different implementation.
Also, it is possible to cast subclass' object to superclass' and call superclass' methods.
Subclass is basically "a super class" with added functionalities.
So when we call superclass method ( typically overridden method), the sub class overridden method
at least 'should match' its superclass method.
The subclass method should 'at least' be same as its superclass
with respect to access and also exceptions thrown if any.
Again, if subclass throws same or more specific ( i.e. subclass of exception), it is possible to cast to its superclass.
but not vice versa. So the restictions for exceptions and also for access.
i.e Overridden method is 'at least' have access of its super class method or more 'access' but not less.
Pardon me if I confused you more!

Regards,
Anand Kulkarni
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rishi,
This is what the book "The Java Programming Language, 3rd Edition" book says:


..code written to deal with the original method declaration will not be prepared to catch any additional checked exceptions and so no such exceptions are allowed to be thrown.


Consider the following code. If it is allowed for overriding method to throw more checked exception, then line 2 will fail.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic