Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic