• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Virtual method Invocation

 
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.





 
Sheriff
Posts: 17281
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic