Java super Keyword: Calling Parent Class in Inheritance, Must-Know

In Java, when we create an instance of a subclass, the subclass inherits all non-private members (member variables and member methods) from the parent class. However, sometimes we need to explicitly call the parent class’s constructor, member variables, or member methods, and that’s where the super keyword comes in. The super keyword acts as a “bridge” between the subclass and the parent class, enabling us to access the parent class’s members within the subclass.

I. Using super to Call the Parent Class Constructor

When a subclass constructor is executed, it implicitly calls the parent class’s no-argument constructor (via super()). If the parent class has no no-argument constructor, or if the subclass needs to call the parent class’s parameterized constructor, you must explicitly use super(parameter list). Crucially, super() or super(parameters) must be placed on the first line of the subclass constructor.

Example: Calling the Parent Class’s Parameterized Constructor

// Parent class: Animal
class Animal {
    String name;
    // Parent class has only a parameterized constructor (no no-argument constructor)
    public Animal(String name) {
        this.name = name;
        System.out.println("Parent class Animal's parameterized constructor is called, name=" + name);
    }
}

// Subclass: Dog, inherits from Animal
class Dog extends Animal {
    int age;

    // Subclass constructor must call the parent class's parameterized constructor
    public Dog(String name, int age) {
        super(name); // Explicitly call the parent class's parameterized constructor, must be first!
        this.age = age;
        System.out.println("Subclass Dog's constructor is called, age=" + age);
    }

    public static void main(String[] args) {
        Dog dog = new Dog("Wang Cai", 3);
    }
}

Output:

Parent class Animal's parameterized constructor is called, name=Wang Cai
Subclass Dog's constructor is called, age=3

Key Points:
- If the parent class has no no-argument constructor, the subclass must use super(parameters) to explicitly call the parent class constructor; otherwise, a compilation error occurs.
- super() must be placed on the first line of the subclass constructor, or it will cause an error.

II. Using super to Access the Parent Class’s Member Variables

When a subclass member variable has the same name as a parent class member variable, directly accessing the variable will default to the subclass’s variable. To access the parent class’s variable with the same name, use super.variableName.

Example: Accessing the Parent Class’s Member Variable

// Parent class: Person
class Person {
    int score = 80; // Parent class member variable
}

// Subclass: Student, inherits from Person
class Student extends Person {
    int score = 95; // Subclass member variable (same name as parent class)

    void printScores() {
        System.out.println("Subclass score: " + score); // Access subclass variable
        System.out.println("Parent class score: " + super.score); // Access parent class variable
    }

    public static void main(String[] args) {
        Student stu = new Student();
        stu.printScores();
    }
}

Output:

Subclass score: 95
Parent class score: 80

Key Points:
- super.variableName explicitly specifies the parent class’s member variable, avoiding conflicts with subclass variables of the same name.

III. Using super to Call the Parent Class’s Member Methods

When a subclass overrides a parent class method, the subclass method will by default call the subclass’s own method. To call the parent class’s overridden method, use super.methodName().

Example: Calling the Parent Class’s Overridden Method

// Parent class: Animal
class Animal {
    void eat() {
        System.out.println("Parent class: Animals are eating");
    }
}

// Subclass: Dog, overrides the parent class's eat() method
class Dog extends Animal {
    @Override
    void eat() {
        System.out.println("Subclass: Dog is eating bones");
        super.eat(); // Call the parent class's overridden eat() method
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
    }
}

Output:

Subclass: Dog is eating bones
Parent class: Animals are eating

Key Points:
- super.methodName() calls the parent class’s method within the subclass method, even if the subclass has overridden it.

IV. Precautions

  1. Cannot use super in static methods
    Static methods belong to the class, while super is instance-specific. Since static methods have no concept of a “current instance,” super cannot be used here.

  2. super() must be the first line in the subclass constructor
    Otherwise, a compilation error occurs because the parent class’s constructor must initialize first.

  3. super and this cannot be used together in constructors
    Both this() and super() must be placed on the first line of a constructor, so they cannot be called simultaneously.

Summary

The super keyword is essential in Java inheritance, primarily used for:
- Calling the parent class constructor (super() or super(parameters));
- Accessing the parent class’s member variables with the same name (super.variableName);
- Calling the parent class’s overridden member methods (super.methodName()).

Mastering super helps you clearly control the relationship between subclass and parent class members, making it a fundamental skill to understand Java inheritance!

Xiaoye