posted 18 years ago
Hi,
In the above code, you have to understand the overriding concept of methods.See the below example
For the above program,it will print derived three times.It will always call the print() in derived class,bcoz you have overridden it.
In the same way for the program which you gave ,at the statement
bogo(new Extension()); This will goto constructor of Extension and then u can assume super(); by default ,so it will goto base class constructor now it will call add(1),then the method in Extension class will be called.
Now i value is 2. Again it will come back to Extension constructor ,now it will call add(2) ,now the method in Extension will be executed and i value becomes 6. Now the creation of the instance (new Extension()) is over. It will go into the method. Now b.add(8) will be called.Now also the method in Extension class will be called,bcoz you have overridden the method add().i value becomes 22.
I think this explaination might have helped u.If I am wrong,correct me.