Constructor Chain
Who is born first: the parent or the child? Master the Constructor Chain to understand the precise order of object creation.
When you create a child object (like a SmartPhone), the computer doesn't just create the child. Because a SmartPhone IS-A Phone, the Phone part must also be set up.
This process of parents being built before children is called the Constructor Chain.
The Essentials
The "Order of Creation" guide:
- Top-Down: Constructors always execute from the top of the family tree down to the bottom.
- Implicit Call: Even if you don't write it, Java automatically calls
super()as the very first line of every child constructor. - Parent First: You cannot set up a child until its "foundation" (the parent) is ready.
- Manual super(): If a parent's constructor requires arguments, you must call
super(args)manually as the first line.
The House Analogy: Foundation Before Walls
Imagine you are building a house with a basement.
- The Basement (Parent): You must dig the hole and pour the concrete first.
- The First Floor (Child): Only after the basement is solid can you start building the walls on top of it.
Inheritance works the same way. The Parent is the "Basement" of the Child. If you try to build the child first, the whole structure would collapse.
Constructor Chain in Action: The Order Problem
Trying to use parent data before the parent is initialized. This is impossible and leads to compile-time errors.
constructor() {
this.name = "Rex";
super(); // ERROR!
}
}
You cannot paint the walls before the basement is poured. super() must always come first.
The parent is built first. Once the foundation is solid, the child can safely add its own unique setup.
constructor() {
super(); // Foundation first
this.bark();
}
}
Execution flows Top-Down. Animal setup completes, then Dog setup begins. Safe and predictable.
Code Implementation: The "Super" Call
Here is how the chain of execution looks across different levels of inheritance:
class LivingBeing {
constructor() { console.log("1. LivingBeing initialized"); }
}
class Animal extends LivingBeing {
constructor() {
super(); // Call LivingBeing
console.log("2. Animal initialized");
}
}
class Dog extends Animal {
constructor() {
super(); // Call Animal
console.log("3. Dog initialized");
}
}
const d = new Dog();
// Output: 1, then 2, then 3The First Line Rule
In Java, super() must be the very first line of your constructor.
- Why? Because you might want to use a parent's attribute inside the child constructor. If the parent isn't built yet, that attribute won't exist.
- The Constructor Stack: When you call
new Dog(), the Dog constructor starts, immediately "pauses," calls the Animal constructor, and only finishes once Animal is done.
By understanding the constructor chain, you can predict exactly how your objects are initialized, avoiding the common "Null Pointer" errors that happen when a child tries to use a parent that isn't fully born yet.
Now that we know how objects are born, let's learn how they can take multiple forms: Polymorphism.
Practice what you just read.