This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked 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: 61456
    
  67

Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place. Please read this for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Polymorphic reference Exception question