Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Doubt on Over-ridden code

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23908
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Abhi...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Friends.
I got it.
@ nagarjuna
Yeah, it will compile if throw is removed, for sure.
Thanks
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again: Please use code tags when you post code.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic