Java OOP · Module 6
The constructor chain and super
super(...) to build the parent, super.method() to extend it
What you'll be able to do
- Trace the order in which parent and child constructors run
- Call a parent constructor explicitly with `super(...)`
- Explain the error you get when the parent has no no-argument constructor
- Extend inherited behaviour with `super.method()` instead of duplicating it
super(...) Constructors
Every object is built from the top down
Constructors are not inherited — but they *are* chained. A Dog cannot exist before the Animal part of it exists.
A Dog object physically contains the Animal part: its fields, its methods. So when you write new Dog(...), Java must initialise the Animal part first, then the Dog part. That is the constructor chain.
super(...) calls a constructor of the parent class. Three rules govern it:
1. If you write nothing, Java silently inserts super(); — a call to the parent's no-argument constructor.
2. If the parent has no no-argument constructor, that silent insertion fails and you get a compile error — you must call super(...) explicitly with real arguments.
3. super(...) must be the first statement in the constructor (the same rule as this(...) from Module 3, and you cannot use both in one constructor).
A child constructor that does its job
name up to Animal(String name), which assigns the inherited field. The Dog does not assign name itself — the parent owns that field and its rules.Predict the order of the three lines before running. Then delete `super(name);` and read the compile error — it explains rule 2 better than any textbook.
The error every beginner meets exactly once
constructor Animal in class Animal cannot be applied to given types; required: String, found: no arguments. Translation: *you forgot super(...), and I could not guess the parent's data for you.* Add the explicit call — or give the parent a no-argument constructor if a nameless Animal is genuinely valid.
Prove it — coding challenges
super.method() Calls
super.method(): extend, do not duplicate
A manager's salary is the base salary **plus** a bonus. Copy the base-salary formula into `Manager` and you now maintain it twice.
When a child replaces a parent's method (overriding — Module 7 goes deep on it), it often wants to *add to* the parent's work rather than throw it away. super.method() runs the parent's version from inside the child's version.
Think of it as 'do what my parent does, then do my extra part'. Without it, you copy the parent's logic — and the copy drifts out of date the day the parent changes.
Change the base salary formula in `Employee.salary()` (say `baseSalary * 1.1`) and re-run: the Manager's total updates automatically. That is the payoff.
Did you notice something strange?
print() is declared only in Employee, yet for omar it printed 6500.0 — the *Manager's* salary. The parent's method called salary() and Java chose the child's version at runtime. That is dynamic dispatch, and it is the heart of Modules 7 and 8.
Three uses of `super`
Prove it — coding challenges
Common mistakes
Dog(String name) { this.name = name; } // parent has Animal(String)Dog(String name, String breed) { this.breed = breed; super(name); }Copying the parent's formula into the override instead of calling `super.salary()`.Lesson playground
A SportsCar that boosts the inherited accelerate(). Add a `brake()` in Vehicle and override it in SportsCar so it also prints 'Spoiler deployed'.
Check understanding
Quiz
Question 1 of 5
Nobody writes super() anywhere. In what order do the messages appear?
Summary
- Objects are constructed top-down: the parent part first, then the child part.
- `super(...)` calls a parent constructor and must be the first statement; Java inserts `super()` when you omit it.
- If the parent has no no-argument constructor, an explicit `super(...)` is mandatory.
- `super.method()` reuses the parent's implementation so shared logic lives in one place.