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
posted
0
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
posted
0
Ow okay , Got it . Thank You Simran!
Jeevan Reddy
Ranch Hand
Joined: Nov 10, 2009
Posts: 142
posted
0
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