Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding Confusion

 
Pritish Chakraborty
Ranch Hand
Posts: 91
C++ Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
C++ Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Junilu Lacar
Bartender
Posts: 7480
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic