• 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

Overriding Confusion

 
Ranch Hand
Posts: 91
Firefox Browser C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all. I've gone through K&B completely, and I'm finally back for my first query .

In Chapter-2, it has been mentioned in an ExamWatch box that if a method is overriden but we use a supertype reference to refer to the subtype object with the overriding method, the compiler assumes that we're calling the supertype version of the method. Aight. This has me a bit confused, and I want to know whether I've interpreted it right. We have a Horse and Animal example given as follows (pardon the C++ style indentation, I can never let go of that...lol) -:



As per the bold text, the compiler assumes that we're calling the supertype version of the eat() method. Yet, the output is -:
Generic
Omnomnom

This seems to contradict what's given in the box.
If I've interpreted this correctly, does this mean that at runtime, the compiler's understanding is overruled and b is actually pointing to a Horse object is found out?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right. compiler does not know what type of object is on the other end of the reference. Try doing one thing and then you will understand. in your animal eat method, declare the method to throw checked exception as :



Now you see what happens. And do post the your understanding and thoughts here.
 
Pritish Chakraborty
Ranch Hand
Posts: 91
Firefox Browser C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just did what you said, gurpeet! I got an exception on both a.eat() and b.eat(). This confirms what we discussed, that the compiler cannot differentiate on basis of object type being referred to, it thinks that both a and b are pointing to Animal objects, hence b.eat() also throws an exception!
Thanks...this clears everything. I'll be back with more
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the main catch over here is the compiler does not know what the reference variable is pointing too. It just checks the type of the reference variable and does a syntax check for the methods inside the type declared.



Change a.eat() in main() to a.eateat() and see.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pritish Chakraborty wrote:I just did what you said, gurpeet! I got an exception on both a.eat() and b.eat(). This confirms what we discussed, that the compiler cannot differentiate on basis of object type being referred to, it thinks that both a and b are pointing to Animal objects, hence b.eat() also throws an exception!
Thanks...this clears everything. I'll be back with more



The "exception" you got on a.eat() and b.eat() is a compiler error, not an exception. There's a big difference. And it has nothing to do with what you appear to think it does. Edit: well, I guess it may have something to do with the issue you're asking about. If you keep the throws Exception on Line 14 and change the declaration on Line 6 to Horse b = new Horse(), you will only get a compiler error on Line 7.
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic