File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 924
    
    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.
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: 924
    
    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: 5226
    
  12

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