M
Marc Twain
This is a bit of a tricky question... but I'm sure the gurus among you
will know the answer
From the reference I learned that when an object is created, the
following occurs:
1- The memory space is allocate and initialized to default
2- Explicit initialization is performed
3- A constructor is called.
So far, so good. Now what happens when dealing with subclasses? I
would assume that step 1, 2 are executed for the current class, then
step 3 kicks in. Now because this is a subclass, the parent
constructor is called as the first line in the constructor block,
either implicitly or explicitly (if the 'no arg' constructor in the
parent class is absent).
Then the parent class would go through steps 1, 2 and 3 again.
Sounds good? Well the reference states something different. It says
that step 1, 2, 3 occur for the subclass, then after the call to the
parent constructor ONLY step 2 and 3 kick in for the parent class.
Which does not make any sense, since step 2 (Explicit init of the
member variables) cannot take place without step one (Memory space
allocation?).
So what it is? Does Java magically traverse the class hierarchy in
Step one, looking for any possible parent variables? Or is this just a
typo?
TIA,
AJ
will know the answer
From the reference I learned that when an object is created, the
following occurs:
1- The memory space is allocate and initialized to default
2- Explicit initialization is performed
3- A constructor is called.
So far, so good. Now what happens when dealing with subclasses? I
would assume that step 1, 2 are executed for the current class, then
step 3 kicks in. Now because this is a subclass, the parent
constructor is called as the first line in the constructor block,
either implicitly or explicitly (if the 'no arg' constructor in the
parent class is absent).
Then the parent class would go through steps 1, 2 and 3 again.
Sounds good? Well the reference states something different. It says
that step 1, 2, 3 occur for the subclass, then after the call to the
parent constructor ONLY step 2 and 3 kick in for the parent class.
Which does not make any sense, since step 2 (Explicit init of the
member variables) cannot take place without step one (Memory space
allocation?).
So what it is? Does Java magically traverse the class hierarchy in
Step one, looking for any possible parent variables? Or is this just a
typo?
TIA,
AJ