wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overridding and Exceptions in KB book 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 "Overridding and Exceptions in KB book" Watch "Overridding and Exceptions in KB book" New topic
Author

Overridding and Exceptions in KB book

sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
I Have a doubt or i have not understood this properly. In KB book page 102 it states

"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
."

and in exam watch on page 104 its given:"

class Animal {
public void eat() throws Exception {
// throws an Exception
}
}
class Dog2 extends Animal {
public void eat() { // no Exceptions }
public static void main(String [] args) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat(); // ok
a.eat(); // compiler error -
// unreported exception
}
}
This code will not compile because of the Exception declared on the
Animal eat() method. This happens even though, at runtime, the eat() method used would be the Dog version, which does not declare the exception
."

I feel whats stated in page 102 is contrary to the exam watch in page 104. If i have not understood this properly can somebody please explain..Thanks
Shilpa Pradhan
Greenhorn

Joined: Oct 17, 2005
Posts: 9
Compilation error is not because Dog's eat() method is not throwing any exception,but because you are not catching exception when you are calling a.eat();

Beacause when you say a.eat() even if the dog's eat() is getting called, compiler thinks that you are making a call to Animal's eat() which throws exception.So compiler thinks that you need to catch that exception and gives error.

Here compilation error has nothing to do with overriding.
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
well according to the Bottomline(my above post)..it says the overriding method need not declare any exception and thats what the code is doing..so why catch it if theres no need to declare it? and what if the superclass is not visible for some reason?
Shilpa Pradhan
Greenhorn

Joined: Oct 17, 2005
Posts: 9
There is no need to catch it. Thats the reason
d.eat(); // ok
is working fine.

But when you call a.eat(); compiler thinks that you are calling Animal's eat() (Which throws exception). So it expects you to catch that exception. (even though the actual object in 'a' is Dog).
Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
The compiler doesn't know at compile time if the interface Animal has any sub classes or not so at compile time it assumes if you say, a.eat(); you're calling the eat method of the Animal class.

But at runtime, it's a different story. The reference variable is of the type Animal but the object is of the type Dog so the eat() method of the Dog class is executed.
Swati Kadam
Ranch Hand

Joined: Mar 05, 2008
Posts: 39
Thank you Shridhar and Shilpa...this post helped me to clear me the concept of exception and overriding


SCJP / Preparing For SCWCD
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
ok from what i have understood from the above posts is that the compiler checks to see if there is eat() method in Animal class and also it finds that the eat method throws an Exception. Now my question is, since eat method throws exception can i catch it in the Animal eat method? will the compiler check for the code inside the eat method. My other question is since it is already declared in the eat method why does it still give a compiler error? Wont the rule 'declare or handle' hold good for this case?

I understand that if i put a try/catch block in the main method where the methods are invoked it compiles fine. Does that mean if exception is thrown in in a method and the method declares the exception but does not handle it, it should be handled in the next method in the stack? cant the method declare, throw and handle the exception all at the same time?
 
Consider Paul's rocket mass heater.
 
subject: Overridding and Exceptions in KB book
 
Similar Threads
Doubt in K&B SCJP 5: topic _OVERRIDING
Overriding doubt in K&B
Declaring exceptions in overriding methods
Please explain this issue related to overriding
Exception in Overriding