# Sub class methods

preeti khane

Ranch Hand

Posts: 93

Reshma Shanbhag

Ranch Hand

Posts: 204

posted 13 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 ]

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 ]

SCJP 1.4, SCWCD 1.4