Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 6

The constructor chain and super

super(...) to build the parent, super.method() to extend it

22m55 XP
→→→

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
1

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

Main.java
1class Dog extends Animal {
2 private String breed;
3
4 Dog(String name, String breed) {
5 super(name);
6 this.breed = breed;
7 }
8}
Dog(String name, String breed) {
The child's constructor takes everything needed to build both parts of the object — the parent's data and its own.
super(name);
Hands 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.
this.breed = breed;
Only after the parent part is complete does the child initialise its own field. Reversing these two lines is a compile error.
Watch the chain run

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.

Output
Press Run to execute this example.

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

2

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.

Base salary + bonus, written once

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.

Output
Press Run to execute this example.

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`

super(args)
Calls a parent constructor. First statement only.
super.method()
Calls the parent's version of a method, bypassing the override. Legal anywhere in an instance method.
super.field
Reads the parent's field when the child declares a field of the same name (shadowing — avoid doing this on purpose).

Prove it — coding challenges

Common mistakes

WrongDog(String name) { this.name = name; } // parent has Animal(String)
WhyJava inserts `super();`, the parent has no no-argument constructor, and compilation fails — even though the assignment itself looks fine.
FixDog(String name) { super(name); }
WrongDog(String name, String breed) { this.breed = breed; super(name); }
Why`super(...)` must be the very first statement.
FixPut `super(name);` first, then the child's own assignments.
WrongCopying the parent's formula into the override instead of calling `super.salary()`.
WhyTwo copies of one rule; the child silently goes stale when the parent changes.
Fixreturn super.salary() + bonus;

Lesson playground

Free practice

A SportsCar that boosts the inherited accelerate(). Add a `brake()` in Vehicle and override it in SportsCar so it also prints 'Spoiler deployed'.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 5

Nobody writes super() anywhere. In what order do the messages appear?

Main.java
1class A {
2 A() {
3 System.out.println("A");
4 }
5}
6
7class B extends A {
8 B() {
9 System.out.println("B");
10 }
11}
12
13class C extends B {
14 C() {
15 System.out.println("C");
16 }
17}
18
19public class Main {
20 public static void main(String[] args) {
21 C c = new C();
22 }
23}

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.