aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding doubt" Watch "Overriding doubt" New topic
Author

Overriding doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
Can anybody explain me the below mentioned statements with a example.

1.The overriding method must not throw checked exceptions that are new or broader than those declared by the overridden method.

2.The overriding method can throw narrower or fewer exceptions.



Thanks All
Arora
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
Hi All,
Can anybody explain me the below mentioned statements with a example.

1.The overriding method must not throw checked exceptions that are new or broader than those declared by the overridden method.

2.The overriding method can throw narrower or fewer exceptions.

Thanks All
Arora






cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks chandra and one more doubt. If a class implements interface the explanation what you gave remains same to interfaces also.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Yeah Nikhil! That is true!

See the description code below, you will have some more idea regarding that!



Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

two remarks:
the fact that the overridden method may throw narrower exceptions includes the case that it may not throw any exception at all.
And be carefull when you use these methods polymophically. For the exception to be handled (or not) only the reference type counts, not the actual type of the object.


prints
without exception
without exception


Yours,
Bu.


all events occur in real time
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks Hassel and Chandra
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Hassel and chandra,
What about Runtime Exception?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
The same is true for RuntimeExceptions. Only difference is they are no checked exception, so they don't need to be declared or handled.

Bu.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Hassel,
Can you give me a example please?
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Chandra,
p1.readit(); //Need not handle or declare, because //accessed using class ref variable and that says to//throw unchecked exception

I didnot get the above statement please explain
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
Hi Chandra,
p1.readit(); //Need not handle or declare, because //accessed using class ref variable and that says to//throw unchecked exception

I didnot get the above statement please explain




Modifiable interface readit() declares the IOException that it throws
it. IOException is the checked exception. The implementing class Program is
free to declare that exception in the method or not. But it can't
throw wider or new checked exception. What can it only throw are:

1- The same exception thrown by the interface method
2- Or subclass of it
3- Or unchecked exceptions. (any number of)

In our case we say that we will will implement the interface method readit()
following 3rd case.

Important Line:Whether the exception to be handled (or not) only the reference type matters, not the actual type of the object. [/b]

p1.readit();

p1 is the reference variable of the Program class, Program class implements the method readit() with unchecked exception. So I said we need not to declare or handle it. (Because compile can only see at the reference type,
reference type method does not throw the checked exception, so no problem of handling or declaring it). OK?

We have to bother about handling or declaring rule when the reference type is interface Modifiable; Because Modifiable says that readit() throws IOException (check exception)

Modifiable m1;
m1.readit(); //ERROR, you must handle or declare it (in our case main() method should declare it or place this statement inside the try catch )
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hey got it and anything else if you know regarding this please share
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding doubt
 
Similar Threads
Regarding Overiding
Overriding method doubt
Overriding and exception
overridden method
Can finalize() throw Exception?