• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q about accessing variables and methods in a polymorphic way

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got this code here in Javaranch:


When I run this code it produces the result below:
2
-2
B
1
-2
A

I was wondering why did a.i printed out the value 1, instead of 2? I thought when A a = b is executed, the actual object type will be used (and that is B).

[ March 30, 2005: Message edited by: Paulo Aquino ]
[ March 31, 2005: Message edited by: Paulo Aquino ]
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paulo,
when ever you are trying to access like that it will be like that.
what i mean is:

Here u have created an object for class B, and if u call overriding method, or if u call the variable or if u call the static method, it will call the derived class things, other wise if u assaign the reference of derived class to the reference of a base class, and trying to access, then it will call the derived class method, and the base class variable and base class static method.

for variables, it will always looks for the reference it is pointing to, and for methods it will always looks for the kind of object it is looking for. so u got the result like that. u cant override static methods, u can simply hide them, so it always looks for the reference type pointing to.

i think u got my point.
sri.
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you are saying that for variables the reference type would be used and not actual object type (polymorphism)?
 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,
thats right.
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...OK. I'm trying to look up that concept from JLS, but I can't see to find it. Care to give me a helping hand? Thanks.
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic