aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions 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 "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Neil Muya
Ranch Hand

Joined: Oct 26, 2009
Posts: 30
Hi,

Just want to clarify something with Exception:



In this code a.eat() calls the eat method of Frog not Animal.

Then why does a.eat() gives a compile error that it should Handle/Declare?

Maybe there is a rule I'm missing . Hope you could help me.


Thanks in Advance!
Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183


Compiler checks the reference type of the variable. In case of overriding which method
will be called is decided at runtime. In the line :

Animal a = new Frog();

Compiler sees that a is of type Animal . But the eat() method in class Animal throws Exception
which is neither handled nor declared in the place from where its called ( main() method here). Hence you get a Compiler error . eat() of Frog class is called at runtime but befor that
compiler checks the method in the class of the reference type which is Animal here
Neil Muya
Ranch Hand

Joined: Oct 26, 2009
Posts: 30
Ow okay , Got it . Thank You Simran!
Jeevan Reddy
Ranch Hand

Joined: Nov 10, 2009
Posts: 142
Simran Dass wrote:

Compiler checks the reference type of the variable. In case of overriding which method
will be called is decided at runtime. In the line :

Animal a = new Frog();

Compiler sees that a is of type Animal . But the eat() method in class Animal throws Exception
which is neither handled nor declared in the place from where its called ( main() method here). Hence you get a Compiler error . eat() of Frog class is called at runtime but befor that
compiler checks the method in the class of the reference type which is Animal here


Thanks for the info Simran. That helped me too.

SCJP 1.6 (94%)
http://faq.javaranch.com/java/JspFaq
 
wood burning stoves
 
subject: Exceptions