Understanding the super Keyword and Constructor Chaining in Java

The super keyword in Java serves a purpose similar to this, but with distinct behavior related to inheritance hierarchies.

this Keyword Review

The this reference operates within instance and constructor methods, pointing to the current object. It cannot be used in static contexts. Common usage includes:

public void assignName(String name) {
    this.name = name;  // Distinguishing parameter from instance variable
}

Constructor chaining within the same class uses this() syntax, which must appear as the first statement in a constructor.

super Keyword Functionality

Like this, super works in instance and constructer contexts but references the parent class components. While often optional, super becomes necessary when accessing overridden parent methods or shadowed fields:

@Override
public void display() {
    super.display();  // Calling parent's version
    // Additional child-specific logic
}

Unlike this which represents the complete object, super points only to inherited characteristics. This distinction means super cannot be direct printed:

System.out.println(this);   // Valid - prints object reference
System.out.println(super);  // Compilation error

Constructor Chaining with super()

When creating subclass instances, Java automatically invokes the parent constructor. This implicit call equivalent to super() occurs at the beginning of each contsructor unless explicitly overridden:

// Parent class
public class Vehicle {
    public Vehicle() {
        System.out.println("Vehicle initialized");
    }
}

// Child class
public class Car extends Vehicle {
    public Car() {
        // Implicit super() call happens here
        System.out.println("Car initialized");
    }
}

Executing new Car() produces:

Vehicle initialized
Car initialized

Explicit super() calls allow parameter passing to parent constructors:

public class Car extends Vehicle {
    private int doors;
    
    public Car(int doorCount) {
        super();  // Explicit parent constructor call
        this.doors = doorCount;
    }
}

Both this() and super() must occupy the first line of a constructor, making them mutually exclusive within the same constructor.

Practical Application Example

Consider a scenario requiring initialization of both parent and child properties:

public class Animal {
    private String species;
    
    public Animal(String speciesName) {
        this.species = speciesName;
    }
    
    public String getSpecies() { return species; }
}

public class Dog extends Animal {
    private int age;
    
    public Dog(String breed, int years) {
        super(breed);     // Initialize parent property
        this.age = years; // Initialize child property
    }
    
    public int getAge() { return age; }
}

This approach maintains clean separation between parent and child initialization while ensuring proper construction sequence.

Tags: java Inheritance Constructors OOP super-keyword

Posted on Tue, 01 Sep 2026 16:36:28 +0000 by shane07