Skip to lesson

Constructor order in inheritance

When you create a child object, Java runs the parent's constructor first, then the child's. In a chain A → B → C, new C() prints from A, then B, then C: top to bottom.

Parent first, then child

A Student object contains a Person part, and the child's code may use the parent's fields. So when you create a child object, Java runs the parent's constructor first, then the child's.

Example 1 · Parent constructor runs first

OrderDemo.javaJava
class Person {    private String name;
    Person(String name) {        this.name = name;        System.out.println("Person: " + name);    }}
class Student extends Person {    private int marks;
    Student(String name, int marks) {        super(name);        this.marks = marks;        System.out.println("Student: " + marks + " marks");    }}
public class OrderDemo {    public static void main(String[] args) {        new Student("Aarav", 91);    }}
Output
Person: Aarav
Student: 91 marks

What this code does

  • super(name) runs Person's whole constructor before the next line of Student's constructor.
  • So the parent's message always prints before the child's.

In a longer chain, Person → Employee → Manager, the calls go up first and the bodies then finish from the top down. Step through it below.

Trace the code

Calls go up, bodies finish top-down

class Person {
Person(String name) {
System.out.println("Person: " + name);
}
}
class Employee extends Person {
Employee(String name, int salary) {
super(name);
System.out.println("Employee: Rs. " + salary);
}
}
class Manager extends Employee {
Manager(String name, int salary, int team) {
super(name, salary);
System.out.println("Manager: team of " + team);
}
}
public class OrderTrace {
public static void main(String[] args) {
new Manager("Rohan", 95000, 8);
}
}

Variables

name
Rohan
salary
95000
team
8

Output

(nothing printed yet)

Line 15new Manager("Rohan", 95000, 8) makes one object and calls Manager's constructor with these values.

Step 1 of 9
Only one object is created: the Manager. Running the parent constructors only builds the parent parts inside it.

The hidden super()

Three classes each print from their constructor, and none of them writes super. If a constructor does not start with super(...) or this(...), Java adds super(); as its first line for you.

Example 2 · Three levels with the hidden super()

ChainOrderDemo.javaJava
class A {    A() {        System.out.println("A constructor");    }}
class B extends A {    B() {        System.out.println("B constructor");    }}
class C extends B {    C() {        System.out.println("C constructor");    }}
public class ChainOrderDemo {    public static void main(String[] args) {        new C();    }}
Output
A constructor
B constructor
C constructor

What this code does

  • No constructor writes super(), but Java adds super(); to the start of B() and C().
  • C() is called first, but A()'s body finishes first: the output goes top to bottom.

Example 3 · Order with this(...) and super(...)

ThisSuperOrderDemo.javaJava
class Person {    private String name;
    Person(String name) {        this.name = name;        System.out.println("Person: " + name);    }}
class Student extends Person {    Student() {        this("Guest");        System.out.println("Student()");    }
    Student(String name) {        super(name);        System.out.println("Student(String)");    }}
public class ThisSuperOrderDemo {    public static void main(String[] args) {        new Student();    }}
Output
Person: Guest
Student(String)
Student()

What this code does

  • Student() calls this("Guest") first, which calls super(name) first. So Person prints first.
  • Then the bodies finish in reverse order of the calls: Student(String), then Student().

If a class has no constructor at all, Java gives it a default constructor (opens in a new tab) that is just C() { super(); }. So every path leads to a parent constructor, all the way up to Object.

When the parent has no no-argument constructor

Person has only Person(String name), and a Student constructor forgets super(...). The hidden super(); looks for Person(), finds none, and javac stops.

Example 4 · Mistake: the parent has no no-argument constructor

MissingSuperDemo.javaJava
class Person {    private String name;
    Person(String name) {        this.name = name;        System.out.println("Person: " + name);    }}
class Student extends Person {    private int marks;
    Student(String name, int marks) {        this.marks = marks;    }}
public class MissingSuperDemo {    public static void main(String[] args) {        new Student("Kavya", 88);    }}
Sample compiler output
MissingSuperDemo.java:13: error: constructor Person in class Person cannot be applied to given types;
    Student(String name, int marks) {
                                    ^
  required: String
  found:    no arguments
  reason: actual and formal argument lists differ in length
1 error

What this code does

  • Java added a hidden super();, but Person has only Person(String). required: String, found: no arguments.
  • The fix is super(name); as the first line. The name parameter was there all along, just unused.

The same error appears when the child has no constructor at all, because the default constructor also calls super();.

The best fix is super(name); with a real value. Adding Person() to the parent also works, but only if a person without a name makes sense.

Fix the error

Fix the missing parent call

Read the error, then pick the line that fixes it.

Round 1 of 3

Vehicle has only the constructor Vehicle(String regNo).

class Vehicle {
Vehicle(String regNo) { }
}
class Car extends Vehicle {
Car(String regNo, int seats) {
System.out.println(seats + " seats");
}
}

Error

Car.java:5: error: constructor Vehicle in class Vehicle cannot be applied to given types;
  required: String
  found:    no arguments
  reason: actual and formal argument lists differ in length

Static blocks and constructors together

Classroom extends School, and both have a static block. Static blocks run once, when a class is first used, and a child needs its parent, so the parent's static block runs first.

Example 5 · Static blocks, then constructors

StaticOrderDemo.javaJava
class School {    static {        System.out.println("School static block");    }
    School() {        System.out.println("School constructor");    }}
class Classroom extends School {    static {        System.out.println("Classroom static block");    }
    Classroom() {        System.out.println("Classroom constructor");    }}
public class StaticOrderDemo {    public static void main(String[] args) {        new Classroom();        new Classroom();    }}
Output
School static block
Classroom static block
School constructor
Classroom constructor
School constructor
Classroom constructor

What this code does

  • The static blocks run once, parent first, the first time Classroom is used.
  • Each new Classroom() then runs School() and Classroom(), in that order.

After that, every new runs the constructors: parent, then child. A field with a starting value, like int fee = 500;, gets it while its own class's part is built, just before that class's constructor body runs. Static blocks (opens in a new tab) has more on static blocks.

Next step

Check your understanding

11 quick questions