Call the parent constructor with super()
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)super(...) inside a child constructor calls the parent's constructor. The child passes values up, and the parent sets its own fields, with its own checks.
Constructors are not inherited
Student extends Person gets Person's methods, but not its constructors: a constructor builds one class only, so new Student("Aarav") cannot use Person(String name) by itself.
The child cannot set Person's private name either. Setters may not exist, and they skip the checks in Person's constructor. The child needs a way to say: "parent, set up your part with these values".
super(...) calls the parent constructor
super refers to the parent-class part of the current object. super(...) calls the parent's constructor from a child constructor, and super.field uses a parent field that a same-named child field hides.
Write super(name); as the first line of the child constructor. Java runs the matching parent constructor, comes back, and only then does the child set its own fields.
Example 1 · super(name) sets a private parent field
class Person { private String name;
Person(String name) { this.name = name; }
String getName() { return name; }}
class Student extends Person { private int marks;
Student(String name, int marks) { super(name); this.marks = marks; }
void show() { System.out.println(getName() + " scored " + marks); }}
public class SuperDemo { public static void main(String[] args) { Student s = new Student("Aarav", 91); s.show(); }}Aarav scored 91
What this code does
super(name)runsPerson(String name), which stores "Aarav" in the privatenamefield.- Then the child sets its own
marks. Each class sets only its own fields.
Example 2 · The parent's checks protect every child
class Account { private final String holder; private int balance;
Account(String holder, int balance) { this.holder = holder; if (balance < 0) { System.out.println("Negative balance, set to 0"); balance = 0; } this.balance = balance; }
void show() { System.out.println(holder + ": Rs. " + balance); }}
class SavingsAccount extends Account { private int rate;
SavingsAccount(String holder, int balance, int rate) { super(holder, balance); this.rate = rate; }}
public class CheckDemo { public static void main(String[] args) { SavingsAccount a = new SavingsAccount("Priya", -500, 4); a.show(); }}Negative balance, set to 0 Priya: Rs. 0
What this code does
- The child passes the bad value up, and the parent's constructor fixes it. The check is written once, in
Account. holderisfinal, so onlyAccount's constructor can set it.super(...)is the only way for the child to give it a value.
The rules for super(...):
- It must be the first statement in the constructor.
- Its arguments must match one of the parent's constructors.
- It can appear only inside a constructor, never in a method.
- A constructor starts with
super(...)orthis(...), never both;this(...)is from Constructor chaining (opens in a new tab).
super(...). Most code, and most interviewers, still expect super(...) first, so write it first.super(...) must come first
A Student constructor sets marks and only then calls super(name). javac refuses: the parent part of the object must be set up before any of the child's own code runs.
Example 3 · Mistake: super(...) is not the first line
class Person { private String name;
Person(String name) { this.name = name; }
String getName() { return name; }}
class Student extends Person { private int marks;
Student(String name, int marks) { this.marks = marks; super(name); }}
public class OrderErrorDemo { public static void main(String[] args) { Student s = new Student("Kavya", 88); }}OrderErrorDemo.java:18: error: call to super must be first statement in constructor
super(name);
^
1 errorWhat this code does
- javac points at
super(name)on line 18: it is the second statement of the constructor, not the first. - The fix: move
super(name);to the top, then setmarks.
If the child ran first, it could read parent fields that are not set yet. Constructor order (opens in a new tab) follows the whole order step by step.
Order of execution
Put the Teacher construction in order
Tap the steps in the order they happen for new Teacher("Meena", "Maths").
Passing values up a chain
In Person → Employee → Manager, the Manager constructor gets a name, a salary and a team size. It keeps teamSize and passes the other two up with super(name, salary).
Example 4 · Passing values up a three-level chain
class Person { private String name;
Person(String name) { this.name = name; }
String getName() { return name; }}
class Employee extends Person { private int salary;
Employee(String name, int salary) { super(name); this.salary = salary; }
int getSalary() { return salary; }}
class Manager extends Employee { private int teamSize;
Manager(String name, int salary, int teamSize) { super(name, salary); this.teamSize = teamSize; }
void show() { System.out.println(getName() + ", Rs. " + getSalary() + ", team " + teamSize); }}
public class ChainSuperDemo { public static void main(String[] args) { new Manager("Rohan", 95000, 8).show(); }}Rohan, Rs. 95000, team 8
What this code does
Managersendsnameandsalaryup toEmployee, which sendsnameup toPerson.- Every value lands in the class that owns its field.
Employee then passes name up with super(name). Each value travels up until it reaches the class that owns its field, so no class ever touches a field that belongs to another class.
super.field: when names clash
A Car declares its own maxSpeed, though Vehicle already has one. In the child, the plain name means the child's field, and super.maxSpeed means the parent's (if it is not private).
Example 5 · super.field reads the parent's field
class Vehicle { int maxSpeed = 100;}
class Car extends Vehicle { int maxSpeed = 180;
void show() { System.out.println("Car: " + maxSpeed); System.out.println("Vehicle: " + super.maxSpeed); }}
public class SuperFieldDemo { public static void main(String[] args) { new Car().show(); }}Car: 180 Vehicle: 100
What this code does
- The
Carobject has twomaxSpeedfields. PlainmaxSpeedinCarmeans the child's field. super.maxSpeedreaches the parent's. This works, but different names would be clearer.
This is a curiosity, not a habit: two fields with one name confuse readers, so give them different names. Field hiding vs method overriding (opens in a new tab) explains this "hiding" in detail.
Next step
Check your understanding
11 quick questions