Java Super Keyword

The super keyword points to the parent class from inside a child class. A child inherits fields and methods from its parent. Sometimes the child needs to reach the parent version directly. Super gives that access.

A Family Example

A daughter learns cooking from her mother. The daughter adds her own spices to the family recipe. When she wants the original recipe, she asks her mother. The keyword super works like that question: it reaches the parent's original version.

Diagram: Child Reaching the Parent

+----------------------+
| Parent               |
|   name = "Parent"    |
|   show()             |
+----------+-----------+
           ^
           |  super.name, super.show(), super(...)
           |
+----------+-----------+
| Child                |
|   name = "Child"     |
|   show()             |
+----------------------+

Three Uses of Super

1. Access a Parent Variable

A child can declare a variable with the same name as the parent variable. Super picks the parent copy.

class Animal {
    String type = "Animal";
}
class Dog extends Animal {
    String type = "Dog";

    void print() {
        System.out.println(type);         // Dog
        System.out.println(super.type);   // Animal
    }
}

2. Call a Parent Method

A child can override a method and still run the parent version. Write super followed by a dot and the method name.

class Vehicle {
    void start() {
        System.out.println("Engine on");
    }
}
class Car extends Vehicle {
    void start() {
        super.start();
        System.out.println("Seat belt check");
    }
}

Calling start on a Car prints "Engine on" and "Seat belt check". The child extends the parent behavior and does not replace it.

3. Call a Parent Constructor

Every child object contains a parent part. Java builds the parent part first. Write super with brackets to choose which parent constructor runs.

class Person {
    String name;
    Person(String name) {
        this.name = name;
        System.out.println("Person ready");
    }
}
class Student extends Person {
    int roll;
    Student(String name, int roll) {
        super(name);
        this.roll = roll;
        System.out.println("Student ready");
    }
}

Creating a Student prints "Person ready" first and "Student ready" second.

Rules for super()

  • The call super() must be the first statement in a constructor
  • Java inserts a hidden super() when you write none
  • The hidden call fails when the parent has no empty constructor, and you must write super(arguments) yourself
  • Super cannot appear inside a static method

Construction Order Diagram

new Student("Ira", 7)
      |
      v
Student constructor starts
      |
      +--- super("Ira") ---> Person constructor runs
      |                          |
      |<-------------------------+
      v
Student constructor continues

Real Example: Employee and Manager

A company stores basic employee data in a parent class. A Manager class adds a bonus. The Manager constructor sends the name and salary upward and keeps the bonus for itself.

class Employee {
    String name;
    double salary;
    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
}
class Manager extends Employee {
    double bonus;
    Manager(String name, double salary, double bonus) {
        super(name, salary);
        this.bonus = bonus;
    }
}

This and Super Compared

KeywordPoints To
thisThe current object
superThe parent part of the current object

Common Mistakes

  • Placing super() below another statement
  • Forgetting that the parent has no empty constructor
  • Using super in a class that has no parent besides Object

Quick Summary

  • super.name reaches a parent variable
  • super.method() runs the parent method
  • super(...) calls a parent constructor and must come first