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).]