posted 24 years ago
The code u have written is quite complicated.Here is the explanation (i think)
1)when u pass the subclass object new Sub1() to m() the constructor for Sub1() will run.In it there is no explicit call to super so compiler inserts it's own call and so Base1() cons. will get called
2)Inside Base1() cons. there is a call to the add() method with param 1.Now here is the trick.The add() method in the SUBCLASS will be called because u have overridden the method.Hence i will get the value
1+(1*2)ie i=3
3)When control comes back to Sub1() cons add() is called with param 2.Hence i now gets the value 3+(2*4)ie i=7
4)Control now passes back to main and add is called with param 8.Hence i now gets the value 7+(2*8)ie i=23
This is an example of rumtime poly.The object makes it's own interpreation of the request u send it.In this case method in SUBCLASS will get called.Don't override and observe result
I hope i have answered correctly.