• 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

Dynamic Method Lookup

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ranchers

can any one explain me these two lines
((STGrandParent)this).getWealth();
System.out.println(((STParent)(this)).wealth);
plz clear my doubts that is that casting of this reference??
Thanks in advance

------------------
Regards
Farrukh Mahmud
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Farrukh.. i am afraid by seeing ur lengthy code .Therefore, consider this short code snippet.It wll cover the logic which u r asking.

1: Methods are called by the actual type of object.
Explanation:
Actual type of the object is the constructor by which abject is created(using new).
i.e if Parent s = new Child();
then new Child() will create the object whose type is Child.i.e actual type of object is Child.
s.method() will call the method of Child Class.
NOTE:First JVM will check method() in Child.If it will not found then it will check it in Parent
2: Variable are called by type of reference.
Example:
if Parent s = new Child();
Then type of reference is Parent.And s.i will refer to the variable present in Parent.
Your Case:
((STGrandParent)this).getWealth(); //Line 1
System.out.println(((STParent)(this)).wealth); //Line 2

In this case this is actually Child refrence.And it is cast in STGrantParent.That is, it will call the variable of STParent & method(getWealth()) of STChild.
Similarly, this is actually Child refrence.And it is cast in STParent.That is, it will call the variable(wealth)) of STParent & method of STChild.
Hope this helps.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!

[This message has been edited by Vikrama Sanjeeva (edited November 21, 2001).]
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Farrukh
1. when you say :
((STGrandParent)this).getWealth();
this is current object is of Child. As subclass can be casted in to base classes *this* is casted to STGrandParent. then runtime enviorenment calls getWealth() method as object is of type Child ,and method is overridden so overridden method of Child is called.
2.System.out.println(((STParent)(this)).wealth);
here first u are casting *this* to STParent and then you are accessing *wealth* instance variable, as object Child will have instance var wealth of STParent also. and it has been casted it, will access instance variable of STParanet.
Remember instance variable are *hide* not overridden.
CMIW
------------------
Regards
Ravish
 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vikrama Sanjeeva
well if u afraid of this code then i think so u should seen longer code then this in yours life. Actually i just cut from my textpad may be it makes it longer
Thanks Kumar nowadays you are really helping me a lot

------------------
Regards
Farrukh Mahmud
 
Vikrama Sanjeeva
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes u r absoloutley right.I have seen much longer code then this.
Just try to ask to the point question, rather making the code complex.Liitle code but full of concept is a worth to understand.
However U R WELLCOME FOR MORE QUERIES
Bye.
Viki.


------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic