← Back to Home

super Keyword

The super keyword in Java is a reference variable used to refer to the immediate parent (superclass) object. It is primarily used in inheritance to access parent class variables, methods, and constructors that are hidden or overridden in the child class.

This is a high-frequency interview topic and critical for understanding inheritance, constructor chaining, and method overriding.

What Is super?

  • Refers to the immediate parent class object
  • Used only in instance context
  • Cannot be used in static context
  • Helps resolve name conflicts between parent and child

Why super Is Needed

class Parent {
    int x = 10;
}
class Child extends Parent {
    int x = 20;
    void show() {
        System.out.println(x);        // 20 (child)
        System.out.println(super.x);  // 10 (parent)
    }
}
          

✔ Without super, parent members hidden by child cannot be accessed.

Uses of super Keyword (Very Important)

1. Access Parent Class Variables

Used when child and parent have variables with the same name.

super.variableName;
          

Example:

class Parent {
    int a = 10;
}
class Child extends Parent {
    int a = 20;
    void display() {
        System.out.println(super.a); // 10
    }
}
          

2. Call Parent Class Methods

Used when a child overrides a method but still wants to call the parent version.

class Parent {
    void show() {
        System.out.println("Parent method");
    }
}
class Child extends Parent {
    void show() {
        super.show();
        System.out.println("Child method");
    }
}
          

✔ Enables method extension, not replacement.

3. Call Parent Class Constructor (super())

Used to invoke the parent class constructor.

class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
}
class Child extends Parent {
    Child() {
        super();
        System.out.println("Child constructor");
    }
}
          

Output:

Parent constructor
Child constructor
          

Important Rules for super()

  1. super() must be the first statement in the constructor
  2. Only one super() call allowed
  3. If not written explicitly, compiler inserts super() automatically
  4. Used to call parameterized parent constructors
super(10);
          

super() vs this()

Feature super() this()
Refers to Parent class Current class
Constructor call Parent constructor Current class constructor
Position First statement First statement
Used for Inheritance Constructor chaining
Combination ❌ Cannot use both together in the same constructor

Where super Cannot Be Used

❌ Static Context

static void test() {
    // super.x; // Compile-time error
}
          

Reason: Static members belong to the class, not objects.

super and Method Overriding (Runtime Polymorphism)

class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
    void callParent() {
        super.sound();
    }
}
          

✔ super bypasses runtime polymorphism and calls parent implementation.

super Keyword vs this Keyword (Interview Favorite)

Aspect this super
Refers to Current object Parent object
Access Current class members Parent class members
Constructor call this() super()
Static usage ❌ No ❌ No

Common Beginner Mistakes

  • Using super in static methods
  • Forgetting super() when parent has no default constructor
  • Calling super() not as first statement
  • Confusing super with this
  • Expecting super to access grandparent directly

Interview-Ready Answers

Short Answer

The super keyword refers to the immediate parent class object and is used to access parent class members.

Detailed Answer

In Java, the super keyword is used in inheritance to access parent class variables, methods, and constructors. It helps resolve naming conflicts, supports constructor chaining, and allows invoking overridden parent methods. super() must be the first statement in a constructor.

Key Takeaway

The super keyword enables controlled access to parent class behavior. Proper use of super ensures correct initialization, clean overriding, and predictable inheritance behavior.