Forums Register Login

Overriding Confusion

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 895 times.
Similar Threads
Runtime and Compiletime
overriding doubt
overloaded method call rules
run-time and compile-time
Refrence type or Instance type
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:09:48.