|
Re: Very simple question I am sure
|
Posted: Dec 2, 2005 9:12 PM
|
|
Your call to super() is working as it is supposed to be. Here is the rule:
If a class inherits another class then the compiler put a call to super() (default consttuctor call of ancestor class) in all the constructors as the first statement if: 1. The first statement in the constructor is not the call to another construtor of the same class, or 2. The first statement in the constructor is not the call to any constructor of ancestor, that is, a statement like super() or super(args list)
The idea here is to call the constructor of ancestor before any code for the class's constructor is executed. Or, putting it another way, the idea is to construtor the object(or you may call it parts that belongs to ancestor) of ancestor before it starts constructing the object of desrived class.
So, in your case, when you call "new TestSuper()", then 1. you have first statement in TestSuper() as this(1,2), which has super() as its first statement, which is used to construct the ancestor part of object. Based on above argument, if you take out the call to super() from TestSuper(int x, int y) then compiler will put this call to super(). To verify this, comment out the call to super() and then you can see your output will be same.
Another idea is to construct an object only once. That is, you cannot call the constructor of ancestor twice by using super() twice or more in your code. This is the reason, compiler lets you put the call to super(...) only as the first statement in teh constructor to make its check easy and enforce the rule that ancestor part must be construted before derived part starts getting constructed.
Thanks Kishori
|
|