Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overridding and Exceptions in KB book

 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Shridhar and Shilpa...this post helped me to clear me the concept of exception and overriding
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic