posted 12 years ago
Hi Preeti,

1.

At //Line 1 when u create an instance of Extension, the super class 0 argument constructor is invoked which invokes the add() method with value 1, because of dynamic binding the add() method of Extension is called and not the add() method of Base.

i=0;

i+= 1*2 ;

Therefore the value of i=2;

2.

The constructor of the Child class has an explicit call to add method with value 2. Hence the value of i becomes 6.

i=2;

i += 2*2;

Therefore value of i = 6;

3.

From the method bogo() the add() method is called with a value of 8.

i=6;

i += 8*2;

Therefore value of i = 22;

when called the 'b.print()' method, it calls the method print() inherited by child 'Extension' and hence prints the value of 22.

Hope this clears your doubt

reshma

[ April 02, 2003: Message edited by: Reshma Pai ]