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

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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic