my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Over-ridden code 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 "Doubt on Over-ridden code" Watch "Doubt on Over-ridden code" New topic
Author

Doubt on Over-ridden code

K Raj Kumar
Greenhorn

Joined: Sep 16, 2008
Posts: 17
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 ]

With Regards,<br />K.Raj Kumar.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18710
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
K Raj Kumar
Greenhorn

Joined: Sep 16, 2008
Posts: 17
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

Joined: Sep 16, 2008
Posts: 509
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

Joined: Oct 16, 2008
Posts: 952
You are right Abhi...


SCJP 6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
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


I know Life is very Tough...But I AM TOUGHER
SCJP 1.5
K Raj Kumar
Greenhorn

Joined: Sep 16, 2008
Posts: 17
Thanks Friends.
I got it.
@ nagarjuna
Yeah, it will compile if throw is removed, for sure.
Thanks
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
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.


thanks
Harvinder
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
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

Joined: Feb 13, 2005
Posts: 75
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

Joined: Aug 16, 2005
Posts: 14104
    
  16

Again: Please use code tags when you post code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on Over-ridden code