Here's what happens when the code is run -
In main() an instance of Extension is passed to bogo . This instance of Extension when passed to bogo results in the Extension object being cast to Base which in turn results in the Base constructor being called . But the add in the Base constructor calls the add() in the Extension class . This is probably 'cause the object passed is of Extension type & add is overridden in it . So now the value of i = 2.
Next the Extension constructor is called (I don't know why !) . The add of the Extension is called here which results in i = 6 .
Since objects are passed by reference the Extension object passed in bogo can be now reffered to by b.
b.add(8) calls the Extension's add method which gives the final value of i = 22 . Note that if add was not overridden in Extension the final value of i would've been 11 .
Since print() is not overridden in Extension the super-class version of print is called & i is printed .
U can see the flow of execution if u have println() statements at the entry & exit of the constructors & methods .
One thing that bugs me though is that the Extension constructor should be called before the Base constructor being called since
new Extension() //initiates a call to the Extension constructor
& if this were to happen i would've been 21 .
Can someone elaborate on this ?
