Ahhh... I got it. First, a critical fact: If the first statement in a derived class constructor is not a call to a base class constructor, the compiler inserts a call to the default base constructor for you. Now, this is what happens:
test.Main is invoked and prints "Before".
new Extension() is executed. It calls Extension.Extension(), which does not call the base class constructor. The compiler therefore inserts a call to Base.Base().
Base.Base() prints "Inside Base func."
Base.Base() calls add(1), and since the object here is actually an instance of class Extension, Extension.add(1) is called.
Extension.add(int) prints "Inside Extension:Add func 1", gives the member i of Base the value 2, and prints "Inside Extension func:Value of i 2". Control passed back to Base.Base().
Base.Base() returns, passing control back to Extension.Extension().
Extension.Extension() now prints "Inside Extension func" and calls add(2). The object here is an instance of class Extension, so Extension.add(2) is called.
Extension.add(int) prints "Inside Extension:Add func 2", gives the member i of Base the value 2+(2*2)=6, and prints "Inside Extension func:Value of i 6". Control passed back to Extension.Extension().
Extension.Extension() is now finished, and returns. Control passed back to test.main.
The newly created Extension object is passed to test.bogo(Base), which calls add(8). Since the object is actually a member of class Extension, Extension.add(8) is called.
Extension.add(int) prints "Inside Extension:Add func 8", gives the member i of Base the value 6+(8*2)=22, and prints "Inside Extension func:Value of i 22". Control passed back to test.bogo(Base).
test.bogo(Base) then calls the print method for the object, which prints the current value of i for the object, 22.
test.bogo is finished, and returns control to test.main.
test.main is finished; the program exits.
Wow...this was good, and I learned something handy. Thanks, Paras!
Hi Roy, That was a good explaination. Thanks!!. But i still have a doubt. When u r in the Base class constructor, when does it call Extension.add() and not Base.add() ? Bye
Originally posted by Roy Tock: Ahhh... I got it. First, a critical fact: [b]If the first statement in a derived class constructor is not a call to a base class constructor, the compiler inserts a call to the default base constructor for you. Now, this is what happens:
test.Main is invoked and prints "Before".
new Extension() is executed. It calls Extension.Extension(), which does not call the base class constructor. The compiler therefore inserts a call to Base.Base().
Base.Base() prints "Inside Base func."
Base.Base() calls add(1), and since the object here is actually an instance of class Extension, Extension.add(1) is called.
Extension.add(int) prints "Inside Extension:Add func 1", gives the member i of Base the value 2, and prints "Inside Extension func:Value of i 2". Control passed back to Base.Base().
Base.Base() returns, passing control back to Extension.Extension().
Extension.Extension() now prints "Inside Extension func" and calls add(2). The object here is an instance of class Extension, so Extension.add(2) is called.
Extension.add(int) prints "Inside Extension:Add func 2", gives the member i of Base the value 2+(2*2)=6, and prints "Inside Extension func:Value of i 6". Control passed back to Extension.Extension().
Extension.Extension() is now finished, and returns. Control passed back to test.main.
The newly created Extension object is passed to test.bogo(Base), which calls add(8). Since the object is actually a member of class Extension, Extension.add(8) is called.
Extension.add(int) prints "Inside Extension:Add func 8", gives the member i of Base the value 6+(8*2)=22, and prints "Inside Extension func:Value of i 22". Control passed back to test.bogo(Base).
test.bogo(Base) then calls the print method for the object, which prints the current value of i for the object, 22.
test.bogo is finished, and returns control to test.main.
test.main is finished; the program exits.
Wow...this was good, and I learned something handy. Thanks, Paras![/B]
I know this is too late to answer this post but this is for others looking for this topic.
First of all whenever a constructor is called than the first thing to be called is the constructor of the base class. If the coder has implicitly provided a call to the constructor than the called constructor will be invoked. Else the default constructor will be called. Remember default construcor has no argument list. So in this case the constructor for Base ith no argument will be called.
Second why add in Extension is called ? this is because the object instance created is of Base but object reference passed is of Extension. So when a method will be called it will be searched in Extension class first because of its reference. Even when you are in any function of Base but call to add(...) will call add(...) in Extension. This is the game of object reference. Hope it will help If I am at any point wrong please correct me. Bye
Hmm. You give a fully-detailed answer to what was clearly a homework question (it's obviously not coming from any real project), to which the original poster had not bothered to give any thought at all, and you thank him?!
Others may disagree, but I think one should avoid giving answers to obvious homework questions, except when the poster shows that they have made a decent attempt at it first.
Remember, your or my company may hire this lazy person, on the strength of a qualification that they got from your work!
... unless your answer was subtly poisoned with wrongness, in which case respect
Hmm. You give a fully-detailed answer to what was clearly a homework question (it's obviously not coming from any real project), to which the original poster had not bothered to give any thought at all, and you thank him?!
Others may disagree, but I think one should avoid giving answers to obvious homework questions, except when the poster shows that they have made a decent attempt at it first.
Remember, your or my company may hire this lazy person, on the strength of a qualification that they got from your work!
... unless your answer was subtly poisoned with wrongness, in which case respect
Agreed 100%....
This is not homework site, where you can find your homework...
You should show your work and then expect for some analysis....
Post by:autobot
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, the permaculture playing cards