Super Keyword in Java Explained
The super keyword in Java is a reference used inside a subclass to refer to its immediate parent (superclass). It is commonly used to access parent class constructors, methods, and variables that have been hidden or overridden.
Calling the Parent Class Constructor
When a subclass is created, super() can be used to explicitly call the parent class's constructor, which must be the first statement inside the subclass constructor.
class Animal {
Animal(String name) {
System.out.println("Animal: " + name);
}
}
class Dog extends Animal {
Dog(String name) {
super(name); // calls Animal's constructor
System.out.println("Dog created");
}
}
Accessing Parent Class Methods
If a subclass overrides a method from its parent, super.methodName() can be used inside the subclass to still call the parent's version of that method.
class Vehicle {
void start() { System.out.println("Vehicle starting"); }
}
class Car extends Vehicle {
void start() {
super.start(); // calls Vehicle's start()
System.out.println("Car starting");
}
}
Accessing Parent Class Variables
super can also be used to access a field from the parent class when the subclass has a field with the same name, avoiding ambiguity between the two.
super()— calls the parent class constructorsuper.method()— calls the parent class's version of a methodsuper.variable— accesses the parent class's field
super keyword is essential in inheritance hierarchies, since it lets a subclass build on top of parent behavior instead of completely replacing it.Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
The Hidden super() Call in Every Constructor
If a subclass constructor does not call super(...) or this(...) on its first line, the compiler inserts super() automatically. That means a parent constructor always runs before the child's constructor body. You can see the order clearly here:
class Person {
Person() { System.out.println("1. Person constructor"); }
}
class Employee extends Person {
Employee() { System.out.println("2. Employee constructor"); }
}
class Manager extends Employee {
Manager() { System.out.println("3. Manager constructor"); }
}
// new Manager(); prints 1, 2, 3 in that order
This automatic call causes a classic compile error. If the parent declares only a constructor with parameters, there is no no-argument constructor for the compiler to call. The fix is to call the right parent constructor explicitly with super(arguments).
Field Hiding Example with super.variable
class Account {
double rate = 3.5;
}
class SavingsAccount extends Account {
double rate = 4.0;
void showRates() {
System.out.println("Child rate: " + rate); // 4.0
System.out.println("Parent rate: " + super.rate); // 3.5
}
}
Fields are not overridden the way methods are; the child's field simply hides the parent's. Hiding fields usually makes code confusing, so in real projects prefer different names or getter methods, and use super.field mainly when reading older code.
super Compared with this
| Point | this | super |
|---|---|---|
| Refers to | The current object | The parent part of the current object |
| Constructor call | this(...) calls another constructor in the same class | super(...) calls a parent constructor |
| Method call | Runs the current class version | Runs the parent class version |
| Position rule | Must be the first statement when calling a constructor | Same rule; you cannot use both in one constructor |
Rules and Mistakes Worth Knowing
supercannot be used inside a static method, because static code has no object.super.super.method()is not allowed; you can only reach the immediate parent.- Private members of the parent cannot be accessed through
super; use protected or public methods instead. - Overriding a method and forgetting to call
super.method()can skip important setup the parent performs, for example in framework lifecycle methods.
There is one more form you will meet with interfaces. When a class implements two interfaces that both provide the same default method, it must override it and can pick one with InterfaceName.super.method():
interface Printer { default void hello() { System.out.println("Printer"); } }
interface Scanner { default void hello() { System.out.println("Scanner"); } }
class Copier implements Printer, Scanner {
public void hello() { Printer.super.hello(); }
}
In everyday code, the most common use is inside an overridden method that adds a little extra behaviour, such as logging or validation, and then hands over to the parent with super.method(). This keeps shared logic in one place, so a bug fix in the parent class automatically benefits every subclass that builds on it.
Checklist: know when the compiler adds super(), call parameterised parent constructors explicitly, and use super.method() when extending, not replacing, parent behaviour.