This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding 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" Watch "Overriding" New topic
Author

Overriding

neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
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 ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

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.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
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
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

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 ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

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):


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
Thank you Roel,good examples.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding
 
Similar Threads
Exception in Overriding
Overridding and Exceptions in KB book
Doubt in K&B SCJP 5: topic _OVERRIDING
Declaring exceptions in overriding methods
Overriding doubt in K&B