Forums Register Login

Overriding

+Pie Number of slices to send: Send
 

The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.



This above quote doesn't fit with below code then what does it mean ?
I thought this should be compile and run without any problem.eat() has been overridden successfully then why compile error ?
+Pie Number of slices to send: Send
 

neha. bajaj wrote:

The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.



This above quote doesn't fit with below code then what does it mean ?
I thought this should be compile and run without any problem.eat() has been overridden successfully then why compile error ?



here compiler is bugging you because at compile-time , compiler has no idea what the reference variable 'a' refers to. it only knows that the type of a is Animal. so at compile-time it 'LOCKS' on the method with the signature eat(), but it has no idea which eat method to call. the superclass version or the subclass version. since the superclass version of eat throws a checked exception, the compiler forces you to either catch or specify the exception in subclass. only at runtime does, the jvm call the overridding eat method which as a matter of fact , does not throw any exception.
+Pie Number of slices to send: Send
 

gurpeet singh wrote: compiler forces you to either catch or specify the exception in subclass. only at runtime does, the jvm call the overridding eat method which as a matter of fact , does not throw any exception.


hi
i had been quoted a statement of book.it is not necessary to declare same exceptions for overriding method.

an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares

 
+Pie Number of slices to send: Send
 

neha. bajaj wrote:

gurpeet singh wrote: compiler forces you to either catch or specify the exception in subclass. only at runtime does, the jvm call the overridding eat method which as a matter of fact , does not throw any exception.


hi
i had been quoted a statement of book.it is not necessary to declare same exceptions for overriding method.

an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares




yes you are right . overriding method does not have to declare any exceptions that it will never throw. in your case you are getting compiler error at a.eat(). so either you put that statement in try-catch block or you should declare the exception to be thrown in your main method. you won't have to declare the exception in overridding eat method. did you get that ?
1
+Pie Number of slices to send: Send
The reason why it doesn't compile is simple and has nothing to do with overriding. First of all you are correct: the eat-method in the Dog2 class is a valid override of eat-method in class Animal. Secondly the reason for a.eat(); failing to compile is that you invoke eat-method on an Animal instance and that method declares a throws-clause, so you have to declare-or-handle that exception.

This will compile successfully (declaring exception):


This will compile too (handling exception):
+Pie Number of slices to send: Send
Thank you Roel,good examples.
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 902 times.
Similar Threads
Declaring exceptions in overriding methods
Exception in Overriding
Overriding doubt in K&B
Overridding and Exceptions in KB book
Doubt in K&B SCJP 5: topic _OVERRIDING
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 00:36:36.