File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding doubt

 
Nik Arora
Ranch Hand
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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




 
Nik Arora
Ranch Hand
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Nikhil! That is true!

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



 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nik Arora
Ranch Hand
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hassel and Chandra
 
Nik Arora
Ranch Hand
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hassel and chandra,
What about Runtime Exception?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hassel,
Can you give me a example please?
 
Nik Arora
Ranch Hand
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey got it and anything else if you know regarding this please share
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic