*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference variable calls a method or instance of the class calls? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference variable calls a method or instance of the class calls?" Watch "Reference variable calls a method or instance of the class calls?" New topic
Author

Reference variable calls a method or instance of the class calls?

vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
Namaste Friends !!

please explain me why " HighDrop 1" is the output following code-:


What I think is obj of Low class does not have access to drop() method of High class and "High hl" reference variable is referring to obj of class Low so it should give compile error...

I am wrong...so please explain me......

Thanks

shiva mistry
Greenhorn

Joined: Feb 05, 2013
Posts: 10
Hi vishal ,Here in this code jvm will invoke method of your reference variable not of the subtype object's method ,it decides at compile time that it has to invoke reference variable's method and your method in subclass is also private, so it is redefining that method not overloading or overriding it.
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
shiva mistry wrote:Hi vishal ,Here in this code jvm will invoke method of your reference variable not of the subtype object's method ,it decides at compile time that it has to invoke reference variable's method and your method in subclass is also private, so it is redefining that method not overloading or overriding it.

Namaste Shiva and

Thank you !!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reference variable calls a method or instance of the class calls?
 
Similar Threads
Please explain this example. Its from Devaka's ExamLab
Java ---== operato
private method and inheritance... Exam Lab...
About GC
help needed on Inner classes program