• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overriding

 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic