• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Overriden method question

 
Ranch Hand
Posts: 486
  • 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.



This Sentence if from Kathy Sierra book on overriden methods.
Can someone please explain me this sentence with example, i am not able to get this please...
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1. If a method in Superclass declares an exception, It is NOT necessary for the overriding method in Subclass to declare an exception. It may decide NOT to declare ANY exceptions at all.
2. But if the overriding method in Subclass MUST declare ANY exceptions, those exceptions have to be a sub-type of exceptions declared by the Superclass method.

So it is like this, either declare an exception that is a sub type of exception declared by the SuperClass Method. Or DO NOT declare ANY exceptions.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This shows that it's perfectly legal for SubFoo.doBar() to declare fewer (no) exceptions than the method it overrides - Foo.doBar().
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the following three cases.

First, the case where the overriding method throws same exceptions as the original one.
It compiles well.

1.


Second, the case where the overriding method throws more exceptions than the original method. This doesnt compile. Check the message which compiler gives.
It says "fun() in B cannot override fun() in A; overridden method does not throw java.lang.Exception
public void fun() throws ArrayIndexOutOfBoundsException,Exception{"

2.


Lastly, the overriding method throws fewer exceptions than the original.
This also compiles fine.

3.

Now do you have any other probable cases.
Well, thats what we say as that the overriding method can throw lesser or fewer exceptions but can never throw more.
The superclass has no knowledge of the newer exceptions which the sub class is going to throw. And the compiler complains.

Hope it helped.

Thanks,
Sandip
 
jaspreet atwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandip Sarkar:
.....
The superclass has no knowledge of the newer exceptions which the sub class is going to throw. And the compiler complains...



Yup, Sandip has pointed the Catch!
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Dinesh,

I would like to make an overall suggestion to help you in your studies. When you have a question like this, go ahead and post some of your own code as part of your question. You're really only going to learn this stuff if you experiment with your own code.

hth,

Bert
 
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