This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding Confusion" Watch "Overriding Confusion" New topic
Author

Overriding Confusion

Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

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?


OCJP 6
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Jun 12, 2012
Posts: 91

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

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding Confusion
 
Similar Threads
overloaded method call rules
Runtime and Compiletime
run-time and compile-time
overriding doubt
Refrence type or Instance type