Java super Keyword

Introduction

The super keyword is one of the most important keywords in Java’s Object-Oriented Programming (OOP) model. It is used to refer to the immediate parent class object and provides access to the members of the superclass. The super keyword is mainly used when inheritance is involved, allowing a child class to access variables, methods, and constructors of its parent class.

For example, if both a parent class and a child class contain a variable named name, the child class can use super.name to access the parent’s variable. Likewise, if a method is overridden in the child class, super.methodName() can invoke the parent class implementation.

The super keyword is also used to call the parent class constructor, ensuring that the superclass is initialized before the subclass. This constructor chaining is performed automatically by the Java compiler if no explicit constructor call is written.

What is the super Keyword?

The super keyword is a reference variable that refers to the immediate parent class object.

It allows a subclass to access members of its superclass.

Definition:

The super keyword is used to refer to the immediate parent class and access its variables, methods, and constructors.

Why Do We Need the super Keyword?

Sometimes a child class defines members with the same names as those in the parent class.

Without super, it becomes difficult to distinguish between them.

Example:


class Animal {
    String type = "Animal";
}
class Dog extends Animal {
    String type = "Dog";
}

Both classes contain the variable type.

Using super.type, the child class can access the parent’s variable.

Syntax

Access a parent variable:


super.variableName;
//Call a parent method:
super.methodName();
//Call a parent constructor:
super();
//or
super(arguments);

Accessing Parent Class Variables

When a parent and child class have variables with the same name, the super keyword accesses the parent’s variable.

Example:


class Animal {
    String type = "Animal";
}
class Dog extends Animal {
    String type = "Dog";
    void display() {
        System.out.println(type);
        System.out.println(super.type);
    }
}
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.display();
    }
}

Output

Dog
Animal

Here:

  • type refers to the child class variable.
  • super.type refers to the parent class variable.

Calling Parent Class Methods

If a child class overrides a method, it can still invoke the parent version using super.

Example


class Animal {
    void sound() {
        System.out.println("Animal Sound");
    }
}
class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog Barks");
    }
    void display() {
        sound();
        super.sound();
    }
}
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.display();
    }
}

Output

Dog Barks
Animal Sound

This allows the child class to extend rather than completely replace the parent behavior.

Calling Parent Class Constructors

The super keyword is commonly used to invoke the parent class constructor.

Example:


class Animal {
    Animal() {
        System.out.println("Animal Constructor");
    }
}
class Dog extends Animal {
    Dog() {
        super();
        System.out.println("Dog Constructor");
    }
}
public class Main {
    public static void main(String[] args) 
        new Dog();
    }
}

Output

Animal Constructor
Dog Constructor

The parent constructor executes before the child constructor.

Constructor Chaining

Constructor chaining is the process where constructors call one another during object creation.

Example:


class Person {
    Person(String name) {
        System.out.println("Person: " + name);
    }
}
class Employee extends Person {
   Employee() {
        super("Rahul");
        System.out.println("Employee Created");
    }
}
public class Main {
    public static void main(String[] args) {
        new Employee();
    }
}

Output

Person: Rahul
Employee Created

The super() call initializes the parent class before executing the child constructor.

Rules of the super Keyword

The following rules apply:

  • super refers only to the immediate parent class.
  • super() must be the first statement in a constructor.
  • If omitted, the compiler automatically inserts super() when the superclass has a no-argument constructor.
  • super cannot be used in a static context.
  • super cannot access private members directly because private members are not inherited.
  • A constructor can call either this() or super(), but not both as the first statement.

super vs this

super this
Refers to the immediate parent class Refers to the current class object
Accesses parent variables Accesses current class variables
Calls parent methods Calls current class methods
Invokes parent constructors Invokes another constructor in the same class
Used with inheritance Used within the current class

Memory Representation


         Dog Object
      ┌───────────────┐
      │ Dog Members   │
      ├───────────────┤
      │ Animal Members│
      └───────────────┘
             ▲
             │
           super

Real-Life Example

Consider an Employee Management System.


class Person {
    String name = "Rahul";
}
class Employee extends Person {
    String name = "Amit";
    void display() {
        System.out.println(name);
        System.out.println(super.name);
    }
}
public class Main {
    public static void main(String[] args) {
        Employee employee = new Employee();
        employee.display();
    }
}

Output

Amit
Rahul

The child class accesses both its own variable and the inherited variable.

Advantages of the super Keyword

Using the super keyword provides several benefits:

  • Accesses parent class variables when names conflict.
  • Invokes overridden parent methods.
  • Supports constructor chaining.
  • Promotes code reuse.
  • Improves readability in inheritance hierarchies.
  • Helps extend parent behavior instead of replacing it completely.
  • Ensures proper superclass initialization.
  • Simplifies object-oriented design.

Best Practices

Follow these best practices when using super:

📖
Best Practices:
  • Use super only when access to the parent implementation is required.
  • Always place super() as the first statement in a constructor.
  • Use @Override when overriding methods that call super.
  • Avoid unnecessary variable shadowing between parent and child classes.
  • Use super.methodName() when extending, rather than replacing, inherited behavior.
  • Keep inheritance hierarchies simple and meaningful.

Common Mistakes

Beginners often make these mistakes:

  • Calling super() anywhere other than the first statement in a constructor.
  • Trying to use super inside static methods.
  • Confusing super with this.
  • Attempting to access private parent members directly using super.
  • Forgetting that constructors execute from parent to child.
  • Assuming super refers to grandparents instead of the immediate parent.
  • Calling both this() and super() in the same constructor.

Conclusion

The super keyword is an essential part of Java inheritance that enables child classes to access the members of their immediate parent class. It is primarily used to access parent variables, invoke overridden parent methods, and call parent class constructors, ensuring proper object initialization and code reuse.

Java super keyword – Interview Questions

Q 1: What is super keyword in Java?
Ans: It refers to the parent class object.
Q 2: Why is super keyword used?
Ans: To access parent class members.
Q 3: Can super call a constructor?
Ans: Yes, using super().
Q 4: Can super be used in static context?
Ans: No.
Q 5: What is difference between this and super?
Ans: this refers to current class, super refers to parent class.

Java Super Keyword – Objective Questions (MCQs)

Q1. What is the purpose of the super keyword in Java?






Q2. Which of the following is a correct use of super to call a parent class constructor?






Q3. Can the super keyword be used to access a parent class method that is overridden in the subclass?






Q4. Which of the following statements about super is TRUE?






Q5. Where must a call to super() appear in a subclass constructor if used?






Related Java Tutorials