• Post Reply Bookmark Topic Watch Topic
  • New Topic

Virtual method Invocation  RSS feed

 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,





The result is blue1. Im trying to understand why, cause I read in Oracle web page, the object you created was a Test object, at run time, when the getColor method was invoked, the runtime reference is to getColor method of a Test class, even though the variable supersub is of the type Testing.

So using Virtual method Invocation will only override the methods, im agree with the blue, but why I received 1..
I thought I will be receiving blue2.

Thanks!

Sorry for to many topics and questions, im preparing to the SCJP, and im reading again chapter by chapter, and also i bought the object oriented programming java book c thomas wu.





 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
number is an instance variable. There is no virtual invocation of instance variables; the reference is resolved at compile time based on the declared type (Testing) of supersub and supersub1. You'll probably get the expected results if you use an accessor, for example a getNumber() method and call that instead of accessing number directly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!