posted 19 years ago
A constructor is called for each class in the chain of superclasses, all the way up to Object.
If you implement a constructor, if the first statement is not a call to one of the superclass's constructors, the compiler inserts a call to the superclass's default (no argument) constructor as the first statement.
The overall effect of these rules is that Object's constructor body executes first, then the next descendent, then the next, down to the class being constructed. If B extends A and C extends B, the constructor body sequence will be Object, A, B, C.
The reason I say "constructor body sequence" is because, technically the call chain is the reverse of the above. C's constructor gets called first, but since the first staetment is a call to B's constructor, the effect is that the body of B's constructor completes before the rest of C's constructor executes. Did I just make that more confusing than it really is?
Long story short, when a class's constructor executes, it can count on the fact that its parent has initialized all of the parents fields.
[ January 30, 2005: Message edited by: David Harkness ]