This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Method Overriding

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

In following code,

public class Base
{
public void aMethod() throws ClassNotFoundException
{
}
}

public class Derived extends Base
{
public void aMethod() throws RuntimeException
{
}
}

Assuming that the classes are in two separate files, compilation of the Derived.java causes

1. A compiler error because RuntimeException is not subclass of ClassNotFoundException
2. No compilation error.

- The answer to the above Question is option 2. but According to my understanding a new overrided method can only specify all or none or a subset of the exception classes specified in the throws clause of the overridden method in the superclass.

and here RuntimeException is definately not subclass of ClassNotFoundException

I am confused...
Please guide
-thanks
S Sethi
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read up on checked vs unchecked exceptions and see if this becomes clear.

For example http://mindprod.com/jgloss/exception.html
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ... this is a good doc. I think i got the point.
this is my understanding...Please do let me know if i am wrong.
"The Overridden method in subclass is throwing unchecked exp... whereas overiding method restriction on exceptions is only for checked exceptions.

I mean - a overridden method in a subclass can throw subtypes of checked exception only but there is no restriction on unchecked exception. Overriding method in subclass is free to declare any new unchecked exception bt not checked exception".

please let me know...if i am close to correct explanation.

thanks
S Sethi
 
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