Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphic reference exception question

 
john smith
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also also, please don't post the same question in more than one forum.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic