Skip to lesson

Call the parent constructor with super()

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

SuperDemo.javaJava
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();    }}
Output
Aarav scored 91

What this code does

  • super(name) runs Person(String name), which stores "Aarav" in the private name field.
  • Then the child sets its own marks. Each class sets only its own fields.

Example 2 · The parent's checks protect every child

CheckDemo.javaJava
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();    }}
Output
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.
  • holder is final, so only Account'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(...) or this(...), never both; this(...) is from Constructor chaining (opens in a new tab).
Java 25 relaxed the first-line rule a little: simple statements that do not use the new object may come before 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

OrderErrorDemo.javaJava
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);    }}
Sample compiler output
OrderErrorDemo.java:18: error: call to super must be first statement in constructor
        super(name);
             ^
1 error

What 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 set marks.

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").

class Person {
private String name;
Person(String name) {
this.name = name;
}
}
class Teacher extends Person {
String subject;
Teacher(String name, String subject) {
super(name);
this.subject = subject;
}
}

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).

new Manager("Rohan", 95000, 8). Manager keeps teamSize = 8 and passes "Rohan" and 95000 up with super(name, salary). Employee keeps salary = 95000 and passes "Rohan" up with super(name). Person keeps name = "Rohan".

Example 4 · Passing values up a three-level chain

ChainSuperDemo.javaJava
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();    }}
Output
Rohan, Rs. 95000, team 8

What this code does

  • Manager sends name and salary up to Employee, which sends name up to Person.
  • 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

SuperFieldDemo.javaJava
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();    }}
Output
Car: 180
Vehicle: 100

What this code does

  • The Car object has two maxSpeed fields. Plain maxSpeed in Car means the child's field.
  • super.maxSpeed reaches 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