• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ow okay , Got it . Thank You Simran!
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic