Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Over-ridden code

 
K Raj Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
Given the code below;


The explanation given is " This code will not compile because of the Exception declared on the Animal eat(). This happens though at runtime, the eat() method used would be the Dog version, which does not declare the exception."

I couldn't able to understand aptly.So, please help Ranchers.
Source: SCJP for Java 6 guide(Kathy Sierra and Bert bates)
Page 108.

I thank in advance and will be much obliged.

[ December 14, 2008: Message edited by: Raj kumar ]

( Jesper Young: Added code tags )
[ December 15, 2008: Message edited by: Jesper Young ]
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it... "a" is a reference to an Animal instance. At compile time, the compiler doesn't know that it's a Dog instance. It may be a Cat instance, and that instance my throw the exception allowed by Animal class.

Henry
 
K Raj Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,
So, you mean to say that, it might throw Exception only, If it's other than dog instance,like cat?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Kumar, remember: overridden methods are invoked based on the object type at RUNTIME. At compile time, the compiler checks whether the class Animal
( which is the declared reference type) has the method eat().So it expects that method eat() will throw an exception. But at runtime eat() of Dog class has been invoked.
I hope...my explanation is right.correct me if I am wrong.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Abhi...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Abhi you wrote compiler expects that method eat() will throw an exception.why it will expect like that,is there any specific reason.



In the above code i removed throw exception from eat method of Animal class then it compiling fine.why?

Thanks & Regards,
beginner in java
 
K Raj Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Friends.
I got it.
@ nagarjuna
Yeah, it will compile if throw is removed, for sure.
Thanks
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nagaraju:
In the above code i removed throw exception from eat method of Animal class then it compiling fine.why?


JLS specifies the handle or declare rule for checked exceptions. i.e. all the exceptions which are direct descendents of Exception class except the RuntimeException class and its subclasses.

It means that if my method throws a checked exception by using the *throws* clause then the code calling this method should either handle that exception using the *try/catch block* or declare to throw the same exception using the throws clause in its method signature.

So in your case, since you commented the eat() method of Animal class not to throw any checked Exception, therefore the compiler never enforced the handle or declare rule upon you.
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what this is means is that a.eat(); should be in a try, catch block correct?
Class Animal
{
public void eat() throws Exception{
// throws exception
}
}
class Dog extends Animal{
public void eat(){
/*no exception*/
}
public static void main(String [] args) {
Animal a = new Dog();
Dog d= new Dog();
d.eat(); //Ok
try{
a.eat();
}catch(Exception e) {
}
}
}

Please correct me if I am wrong?
Thanks,
Meers
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I tried the following:

and got the following output:
Animal's eat
Horses eat hay.

So for polymorphic references, when a method in the super class throws an exception and the method overriding it in the subclass does not., at compile time that exception needs to be handled. This is because the compiler expects the overridden method that declares an exception, although at runtime the eat() is resolved to the Horse -- dynamic binding.
Am I correct?
Thanks,
Meera

( Jesper Young: Added code tags )
[ December 17, 2008: Message edited by: Jesper Young ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again: Please use code tags when you post code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic