posted 21 years ago
Hi Pallavi,
The last two print statements are printing the variables of the object refered by C (one static, one instance). I was trying to show that binding the variables is resolved at compile time as oposed to method binding which is done at runtime.
Therefore because C is a reference to a LtA object (although it points to a LtB object) the static int is of the LtA class and the int i of a LtA instance are printed.
When method m() is called, the visible m() method of the refered object is called and because LtB had overriden m() when extending LtA and because C, although a LtA reference points to a LtB object then the only visible m() method is m() of LtB.
See the code below. It prints the variable corresponding to a LtB instance, the instance C really refers to. That is if you comment line 2. Here although C points to a LtB object the compiler doesn't know who k is because C is a reference to a LtA object and LtA has no k.
A bit too long of an explanation but I hope I was clear.
[ February 04, 2003: Message edited by: Dan Culache ]