Constructor order in inheritance
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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
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); }}Person: Aarav Student: 91 marks
What this code does
super(name)runsPerson's whole constructor before the next line ofStudent'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
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.
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()
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(); }}A constructor B constructor C constructor
What this code does
- No constructor writes
super(), but Java addssuper();to the start ofB()andC(). C()is called first, butA()'s body finishes first: the output goes top to bottom.
Example 3 · Order with this(...) and super(...)
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(); }}Person: Guest Student(String) Student()
What this code does
Student()callsthis("Guest")first, which callssuper(name)first. SoPersonprints first.- Then the bodies finish in reverse order of the calls:
Student(String), thenStudent().
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
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); }}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 errorWhat this code does
- Java added a hidden
super();, butPersonhas onlyPerson(String).required: String,found: no arguments. - The fix is
super(name);as the first line. Thenameparameter 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).
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
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(); }}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
Classroomis used. - Each
new Classroom()then runsSchool()andClassroom(), 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