Web Analytics

The super Keyword

Intermediate ~30 min read

The super keyword is a reference to the parent (superclass) of the current object. It's used to access parent class members (fields and methods) and to call parent class constructors.

What is super?

The super keyword in Java has three main uses:

  1. super() - Call parent class constructor
  2. super.method() - Call parent class method
  3. super.field - Access parent class field

Calling Parent Constructor with super()

Constructors are not inherited in Java. To initialize parent class fields, you must explicitly call the parent constructor using super().

class Animal {
    String name;

    Animal(String name) {
        this.name = name;
        System.out.println("Animal constructor called");
    }
}

class Dog extends Animal {
    String breed;

    Dog(String name, String breed) {
        super(name);  // Call Animal's constructor FIRST
        this.breed = breed;
        System.out.println("Dog constructor called");
    }
}
Important Rules:
  • super() must be the first statement in the subclass constructor
  • If you don't call super() explicitly, Java automatically inserts super() (no-arg) at the beginning
  • If the parent class has no no-arg constructor, you MUST call super(args) explicitly

Constructor Chaining

When you create an object, constructors are called from the topmost parent class down to the child class. This is called constructor chaining.

class Grandparent {
    Grandparent() {
        System.out.println("1. Grandparent constructor");
    }
}

class Parent extends Grandparent {
    Parent() {
        // super() is called automatically
        System.out.println("2. Parent constructor");
    }
}

class Child extends Parent {
    Child() {
        // super() is called automatically
        System.out.println("3. Child constructor");
    }
}

// new Child() prints:
// 1. Grandparent constructor
// 2. Parent constructor
// 3. Child constructor

Calling Parent Methods with super.method()

Use super.methodName() to call a method from the parent class, especially when the method is overridden in the subclass.

class Animal {
    void makeSound() {
        System.out.println("Some generic animal sound");
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
        super.makeSound();  // Call parent's version first
        System.out.println("Woof! Woof!");  // Then add dog-specific behavior
    }
}

Accessing Parent Fields with super.field

When a subclass has a field with the same name as the parent class (variable shadowing), use super.fieldName to access the parent's field.

class Vehicle {
    int maxSpeed = 100;
}

class Car extends Vehicle {
    int maxSpeed = 200;  // Shadows parent's maxSpeed

    void displaySpeeds() {
        System.out.println("Car's maxSpeed: " + maxSpeed);        // 200
        System.out.println("Vehicle's maxSpeed: " + super.maxSpeed);  // 100
    }
}
Best Practice: Avoid variable shadowing as it can be confusing. If you need different values, use different names or override getters instead.
Output
Click Run to execute your code

super vs this

super this
Refers to parent class Refers to current class
super() calls parent constructor this() calls another constructor in same class
super.field accesses parent's field this.field accesses current instance's field
Used when method/field is overridden Used to distinguish instance variables from parameters

Common Mistakes

  • Calling super() after other statements: super() must be the first statement in a constructor.
  • Using both super() and this() in same constructor: You can only use one of them, and it must be the first statement.
  • Forgetting super() when parent has no no-arg constructor: If the parent class only has parameterized constructors, you must explicitly call one with super(args).

Summary

  • super() calls the parent class constructor and must be the first statement
  • super.method() calls the parent class version of an overridden method
  • super.field accesses the parent class field when shadowed
  • Constructor chaining ensures parent classes are initialized first
  • If no super() is written, Java inserts super() automatically