NBKRIST-JavaHub

Java super Keyword — Interactive Learning

Clear examples, expert note, interview practice & summary
🏠 Back to Home

💡 Expert View — Why I Love super

The super keyword is the bridge between a parent class and its subclass. It ensures subclasses are built upon a stable, initialized foundation and allows explicit reuse of parent constructors, methods, and fields.

When an object is created in an inheritance chain, Java initializes the parent part first (via the parent constructor) and then the subclass. super() makes this explicit and controlled. Using super improves clarity when members are shadowed or methods overridden — the intention becomes clear.

1️⃣ Access Parent Class Variables (Fields)

The super keyword lets a subclass access its parent’s variable when it’s hidden by a field of the same name.
Look at Child.java, which illustrates how super accesses the parent's field when the subclass has a field with the same name.

class Parent {
    int value = 100;
}
public class Child extends Parent {
    int value = 200;
    void show() {
        System.out.println("Child value: " + value);
        System.out.println("Parent value: " + super.value);
    }
    public static void main(String[] args) {
        Child obj = new Child();
        obj.show();
    }
}

2️⃣ Invoke Parent Class Methods

When a subclass overrides a method of a superclass, the parent’s version is hidden; using super.methodName() allows calling the parent version explicitly.
Look at Child.java, which demonstrates calling a parent's method from the subclass using super.methodName().

class Parent {
    void display() { System.out.println("Display from Parent"); }
}
public class Child extends Parent {
    void display() { System.out.println("Display from Child"); }
    void show() { super.display(); }
    public static void main(String[] args) {
        Child obj = new Child();
        obj.show();
    }
}

3️⃣ Invoke Parent Class Constructor

The super() call inside a subclass constructor explicitly invokes the matching parent class constructor to ensure proper and controlled initialization.
Look at Child.java, which shows super() used to call a parent constructor.

class Parent {
    // default constructor of the Parent class
        Parent() { System.out.println("Parent (NBKRIST) constructor");
    }
}

public class Child extends Parent {
    // default constructor of the Child class
    Child() {
        super(); // calling parent constructor
        System.out.println("Child (Department of CSE) constructor");
    }
    public static void main(String[] args) {
        new Child();
    }
}

4️⃣ Calling Super Class Constructor with parameters

The super(parameterlist) call in a sub class constructor invokes the matching parent class constructor with the specified arguments.
Look at Child.java, which shows how super is used to call a parameterized parent constructor to build base foundation first.

class Parent {
    String collegeName;
    int establishedYear;

    Parent(String name, int year) {
        collegeName = name;
        establishedYear = year;
        System.out.println("Parent Constructor: " + collegeName + " - Established " + establishedYear);
    }
}

public class Child extends Parent {
    String department;

    Child(String name, int year, String dept) {
        super(name, year); // calling parent constructor with parameters
        department = dept;
        System.out.println("Child Constructor: Department - " + department);
    }

    void displayDetails() {
        System.out.println("College: " + collegeName);
        System.out.println("Year: " + establishedYear);
        System.out.println("Department: " + department);
    }

    public static void main(String[] args) {
        Child c = new Child("NBKRIST", 1979, "Computer Science");
        c.displayDetails();
    }
}

5️⃣ Constructor Chaining Across Inheritance

Constructor chaining across inheritance ensures that when a subclass object is created, constructors from the parent to the child execute in order, forming a complete and consistent object initialization chain.
Look at C.java, which demonstrates how super() ensures constructors run in correct order across inheritance levels.

class A {
    A() { System.out.println("A constructor"); }
}
class B extends A {
    B() { super(); System.out.println("B constructor"); }
}
public class C extends B {
    C() { super(); System.out.println("C constructor"); }
    public static void main(String[] args) {
        new C();
    }
}

💼 Interview Practice Questions

Click on the question to see the correct answer.

🧾 Key Summary