Inheritance allows a child class to reuse behavior from its parent, but sometimes the child needs to explicitly interact with that parent. Java provides the super keyword for exactly this purpose.
The super keyword refers to the immediate parent-class portion of the current object. It allows a child class to access an inherited field, call an inherited method, or invoke a parent constructor.
super is used inside a child class to explicitly refer to its immediate parent class.
Why Do We Need super?
In simple inheritance examples, Java can automatically use inherited members. But situations become more interesting when the child and parent have members with the same name, when a child overrides a method, or when a particular parent constructor must be selected.
In these situations, super removes ambiguity and tells Java that you specifically want to work with the immediate parent class.
Three Common Uses of super
| Usage | Syntax | Purpose |
|---|---|---|
| Parent field | super.field | Accesses a parent field hidden by a child field. |
| Parent method | super.method() | Calls the parent version of an overridden method. |
| Parent constructor | super() | Invokes a constructor of the immediate parent class. |
Using super to Access a Parent Field
Suppose both the parent and child classes declare a field with the same name. Inside the child class, the field declared in the child normally takes precedence. The super keyword allows you to explicitly access the parent version.
class Person { String name = "Parent Name"; } class Student extends Person { String name = "Student Name"; void displayNames() { System.out.println(name); System.out.println(super.name); } } public class Main { public static void main(String[] args) { Student student = new Student(); student.displayNames(); } }
The first statement accesses the Student field because the child declares its own name. The second statement uses super.name to explicitly access the parent class's field.
name → current class field
super.name → immediate parent class field
Using super to Call a Parent Method
The second major use of super appears when a child overrides a parent method but still wants to execute the original parent implementation.
class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void sound() { super.sound(); System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); } }
The child overrides sound(), but super.sound() first executes the parent's implementation. The child can then add its specialized behavior.
Animal makes a sound Dog barks
This pattern is useful when the parent implementation performs important common work and the child needs to extend that behavior rather than completely replace it.
Using super() to Call a Parent Constructor
The third major use of super is constructor invocation. A child constructor can use super() to explicitly call a constructor of its immediate parent class.
class Person { Person() { System.out.println("Person constructor"); } } class Student extends Person { Student() { super(); System.out.println("Student constructor"); } } public class Main { public static void main(String[] args) { new Student(); } }
The parent constructor executes before the child constructor continues.
Person constructor Student constructor
Passing Arguments to super()
A parent class may have a parameterized constructor. In that situation, the child can use super(arguments) to select the appropriate parent constructor.
class Person { String name; Person(String name) { this.name = name; } } class Student extends Person { int rollNumber; Student(String name, int rollNumber) { super(name); this.rollNumber = rollNumber; } void display() { System.out.println(name); System.out.println(rollNumber); } }
Here, super(name) sends the name to the parent constructor. The parent initializes its own state, while the child initializes rollNumber.
super() Must Be the First Statement
When used inside a constructor, a super() call must appear as the first statement in that constructor.
class Child extends Parent { Child() { super(); System.out.println("Child constructor"); } }
You cannot place executable statements before the explicit super() call.
A constructor can explicitly call either super(...) or this(...) as its first constructor-invocation statement, but it cannot use both in the same constructor.
What Happens If super() Is Not Written?
If a child constructor does not explicitly invoke a parent constructor, Java inserts a call to the no-argument parent constructor automatically, provided that such a constructor is accessible and available.
class Parent { Parent() { System.out.println("Parent constructor"); } } class Child extends Parent { Child() { // Java implicitly calls super(); System.out.println("Child constructor"); } }
The important word here is implicitly. Java effectively adds the parent no-argument constructor call when you do not provide another constructor invocation.
When the Parent Has No No-Argument Constructor
This is a classic beginner trap. If the parent class defines only parameterized constructors, Java cannot automatically insert a valid super() call.
class Parent { Parent(String message) { System.out.println(message); } } class Child extends Parent { Child() { // Compile-time error: no matching super() } }
The solution is to explicitly call the available parent constructor.
class Child extends Parent { Child() { super("Parent initialized"); } }
super and this Are Different
The this keyword refers to the current object or current class context, while super refers to the immediate parent-class portion of that object.
| Keyword | Refers To | Common Use |
|---|---|---|
| this | Current class/object | Access current fields, methods, or constructors. |
| super | Immediate parent class | Access parent fields, methods, or constructors. |
Common Beginner Mistakes
- Thinking super refers to any ancestor class instead of the immediate parent.
- Placing super() after another statement inside a constructor.
- Assuming constructors are inherited because super() can invoke them.
- Forgetting to call the appropriate parameterized parent constructor when no no-argument constructor exists.
- Using super when ordinary inherited access would already be clear and sufficient.
Best Practice
Use super when it communicates intent clearly. It is especially valuable when a child overrides behavior but still needs the parent's implementation, or when the parent and child contain fields with the same name.
Avoid adding super everywhere simply because inheritance exists. Good code uses it when the distinction between the child and parent behavior matters.
Interview Insight
A strong interview answer is: "The super keyword refers to the immediate parent class. It is commonly used to access a parent field, invoke an overridden parent method, or call a parent constructor. A super() constructor call must be the first statement in a child constructor."
Final Takeaway
The super keyword gives a child class explicit access to its immediate parent's behavior and initialization. Remember its three core uses: super.field accesses a parent field, super.method() calls a parent method, and super() invokes a parent constructor. Once this distinction becomes natural, method overriding and constructor behavior become much easier to reason about.
