aspose file tools*
The moose likes Beginning Java and the fly likes Polymorphic reference exception question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphic reference exception question" Watch "Polymorphic reference exception question" New topic
Author

Polymorphic reference exception question

john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
(from SCJP for Java 5 by Kathy Sierra and Bet Bates, page 104)

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();
a.eat();
}
}

Question: The book says that the code will not compile because of the Exception declared on the Animal eat() method.

I do not understand why this is. Could somebody please help. Thank you in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

"johnx smithx",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Also, please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

This post has been moved to a more appropriate forum.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11497
    
  16

Also also, please don't post the same question in more than one forum.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, that code is missing the closing brace for Dog2.eat, and therefore won't compile anyway.

Besides that, a.eat() can throw an excecption, but the main method does not - and it doesn't catch it, either.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Originally posted by johnx smithx:
The book says that the code will not compile because of the Exception declared on the Animal eat() method.


That's because the compiler checks the reference variable type (the left side of the equal sign) and sees "Animal a" and Animal eat() method can throw an Exception (despite of the Dog2 instance being referred to - after all it can be Animal or any kind of Animal in runtime).

and as Ilja said, "a.eat() can throw an exception, but the main method does not - and it doesn't catch it, either." So, it won't compile.
[ June 06, 2008: Message edited by: Rodrigo Tomita ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphic reference exception question