← 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

super Keyword Examples (With Output)

1. Using super to Access Parent Variable

class A {
    int x = 10;
}

class B extends A {
    int x = 20;

    void show() {
        System.out.println(super.x);
    }

    public static void main(String[] args) {
        new B().show();
    }
}
          

Explanation: super.x accesses parent variable.

Output: 10

2. Variable Hiding Without super

class A {
    int x = 10;
}

class B extends A {
    int x = 20;

    void show() {
        System.out.println(x);
    }

    public static void main(String[] args) {
        new B().show();
    }
}
          

Explanation: Child variable hides parent variable.

Output: 20

3. Using super to Call Parent Method

class A {
    void show() {
        System.out.println("Parent");
    }
}

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

    public static void main(String[] args) {
        new B().show();
    }
}
          

Explanation: super.show() calls parent version.

Output:

Parent
Child
          

4. Overriding Without super

class A {
    void show() {
        System.out.println("A");
    }
}

class B extends A {
    void show() {
        System.out.println("B");
    }

    public static void main(String[] args) {
        new B().show();
    }
}
          

Explanation: Parent method completely overridden.

Output: B

5. super() Calling Parent Constructor

class A {
    A() {
        System.out.println("Parent Constructor");
    }
}

class B extends A {
    B() {
        super();
        System.out.println("Child Constructor");
    }

    public static void main(String[] args) {
        new B();
    }
}
          

Explanation: super() calls parent constructor.

Output:

Parent Constructor
Child Constructor
          

6. Implicit super() Call

class A {
    A() {
        System.out.println("Parent");
    }
}

class B extends A {
    B() {
        System.out.println("Child");
    }

    public static void main(String[] args) {
        new B();
    }
}
          

Explanation: Compiler inserts super() automatically.

Output:

Parent
Child
          

7. Calling Parameterized Parent Constructor

class A {
    A(int x) {
        System.out.println(x);
    }
}

class B extends A {
    B() {
        super(10);
    }

    public static void main(String[] args) {
        new B();
    }
}
          

Explanation: Required when parent has no default constructor.

Output: 10

8. Compile Error Without super() (Parameterized Parent)

class A {
    A(int x) {}
}

class B extends A {
    // B() {}  // Compile-time error
}
          

Explanation: Parent has no no-arg constructor, so super(x) is mandatory.

9. super() Must Be First Statement

class A {
    A() {}
}

class B extends A {
    B() {
        // System.out.println("Hi");
        // super();   // ❌ Compile-time error
    }
}
          

Explanation: super() must be the first line in constructor.

10. Using super in Multilevel Inheritance

class A {
    void show() {
        System.out.println("A");
    }
}

class B extends A {
    void show() {
        super.show();
        System.out.println("B");
    }
}

class C extends B {
    void show() {
        super.show();
        System.out.println("C");
    }

    public static void main(String[] args) {
        new C().show();
    }
}
          

Explanation: Calls flow upward through hierarchy.

Output:

A
B
C
          

11. super Cannot Access Child Members

class A {}

class B extends A {
    int x = 10;

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

Explanation: super refers only to parent members.

12. super with Method Overriding + Variables

class A {
    int x = 10;

    void show() {
        System.out.println("A");
    }
}

class B extends A {
    int x = 20;

    void show() {
        System.out.println(super.x);
        super.show();
        System.out.println(x);
    }

    public static void main(String[] args) {
        new B().show();
    }
}
          

Explanation: Demonstrates full usage.

Output:

10
A
20
          

13. super with final Method

class A {
    final void show() {
        System.out.println("Final");
    }
}

class B extends A {
    void test() {
        super.show();
    }

    public static void main(String[] args) {
        new B().test();
    }
}
          

Explanation: final methods can be called but not overridden.

Output: Final

14. super with Static Methods (Not Polymorphic)

class A {
    static void show() {
        System.out.println("A");
    }
}

class B extends A {
    static void show() {
        System.out.println("B");
    }

    void test() {
        super.show();
    }

    public static void main(String[] args) {
        new B().test();
    }
}
          

Explanation: Static methods are hidden.

Output: A

15. Constructor Execution Order (Real Interview Favorite)

class A {
    A() {
        System.out.println("A");
    }
}

class B extends A {
    B() {
        System.out.println("B");
    }
}

class C extends B {
    C() {
        System.out.println("C");
    }

    public static void main(String[] args) {
        new C();
    }
}
          

Explanation: Constructors execute top → down.

Output:

A
B
C
          

16. super vs this (Side by Side)

class A {
    int x = 10;
}

class B extends A {
    int x = 20;

    void show() {
        System.out.println(this.x);
        System.out.println(super.x);
    }

    public static void main(String[] args) {
        new B().show();
    }
}
          

Explanation: this → current object, super → parent object.

Output:

20
10
          

17. super in Copy-Style Constructor

class A {
    int x;

    A(int x) {
        this.x = x;
    }
}

class B extends A {
    B(int x) {
        super(x);
    }

    public static void main(String[] args) {
        B b = new B(15);
        System.out.println(b.x);
    }
}
          

Explanation: Parent initialization reused.

Output: 15

18. super Cannot Be Used in Static Context

class A {
    int x = 10;
}

class B extends A {
    static void test() {
        // super.x; // ❌ Compile-time error
    }
}
          

Explanation: super needs object context.

19. Common Interview Trap

class A {
    void show() {
        System.out.println("A");
    }
}

class B extends A {
    static void show() {
        System.out.println("B");
    }
}
          

Explanation: ❌ Not overriding. Static methods cannot override instance methods.

20. Interview Summary – super Keyword

class A {
    int x = 10;
    void show() {
        System.out.println("A");
    }
}

class B extends A {
    void display() {
        System.out.println(super.x);
        super.show();
    }

    public static void main(String[] args) {
        new B().display();
    }
}
          

Explanation:

  • Access parent variables
  • Call parent methods
  • Invoke parent constructors

Output:

10
A
          

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.