File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Method Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Method Overriding" Watch "Method Overriding" New topic

Method Overriding

Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91

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

thanks,<br />Anju Sethi
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Read up on checked vs unchecked exceptions and see if this becomes clear.

For example

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
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.

S Sethi
It is sorta covered in the JavaRanch Style Guide.
subject: Method Overriding
Similar Threads
Runtime exception
doubt from Abhilash's exam
Marcus Green's exam #2
Regarding Overiding
Relationship between Exceptions [Tricky]