Introduction
In Java, the super keyword is a reference variable used to access members (fields, methods, and constructors) of a parent class (also known as the superclass). It plays a crucial role in inheritance, allowing a subclass to interact with its parent class in different ways.
Think of super as a bridge that connects the child class with its immediate parent class.
Why Use super?
- To avoid naming conflicts between superclass and subclass members.
- To invoke the parent class constructor explicitly.
- To reuse code from the parent class without rewriting it.
Types of Usage of super in Java
1. Accessing Parent Class Variables
If a subclass defines a variable with the same name as the parent class, super helps access the parent version.
Example:
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
void displayNames() {
System.out.println("Subclass name: " + name);
System.out.println("Superclass name: " + super.name);
}
}
public class SuperVariableExample {
public static void main(String[] args) {
Dog d = new Dog();
d.displayNames();
}
}
Output:
Subclass name: Dog
Superclass name: Animal
👉 Here, super.name resolves the conflict between the child and parent class variables.
2. Accessing Parent Class Methods
When a subclass overrides a method, super can call the overridden method of the parent class.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
void printParentSound() {
super.sound(); // Calls parent class method
}
}
public class SuperMethodExample {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // Subclass method
d.printParentSound(); // Superclass method
}
}
Output:
Dog barks
Animal makes a sound
3. Calling Parent Class Constructor
A subclass constructor can explicitly call the parent class constructor using super().
If not specified, the compiler automatically inserts a super() call to the default constructor of the parent class.
Example:
class Animal {
Animal() {
System.out.println("Animal constructor called");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls Animal constructor
System.out.println("Dog constructor called");
}
}
public class SuperConstructorExample {
public static void main(String[] args) {
Dog d = new Dog();
}
}
Output:
Animal constructor called
Dog constructor called
4. Calling Parameterized Constructor of Parent Class
You can use super(parameterList) to call a parent class constructor with arguments.
Example:
class Animal {
Animal(String type) {
System.out.println("Animal type: " + type);
}
}
class Dog extends Animal {
Dog(String breed) {
super("Mammal"); // Calling parent constructor with argument
System.out.println("Dog breed: " + breed);
}
}
public class SuperParameterizedConstructor {
public static void main(String[] args) {
Dog d = new Dog("Labrador");
}
}
Output:
Animal type: Mammal
Dog breed: Labrador
5. super with Method Overriding and Polymorphism
In real-world applications, we often use super inside overridden methods to extend functionality instead of completely replacing it.
Example:
class Vehicle {
void start() {
System.out.println("Vehicle is starting...");
}
}
class Car extends Vehicle {
@Override
void start() {
super.start(); // Call parent implementation
System.out.println("Car engine is warming up...");
}
}
public class SuperPolymorphism {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Output:
Vehicle is starting...
Car engine is warming up...
6. Restrictions of super
super()must be the first statement in the subclass constructor (until Java 24).- You cannot use
superin static methods becausesuperrefers to an object instance. supercan only access immediate parent class members, not grandparents directly.
Real-Time Example: Employee Management
Let’s consider a real-world example where super helps reuse parent functionality.
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
void displayInfo() {
System.out.println("Employee: " + name + ", Salary: " + salary);
}
}
class Manager extends Employee {
double bonus;
Manager(String name, double salary, double bonus) {
super(name, salary); // Call parent constructor
this.bonus = bonus;
}
@Override
void displayInfo() {
super.displayInfo(); // Reuse parent method
System.out.println("Bonus: " + bonus);
}
}
public class SuperRealTimeExample {
public static void main(String[] args) {
Manager m = new Manager("Ashish", 80000, 10000);
m.displayInfo();
}
}
Output:
Employee: Ashish, Salary: 80000.0
Bonus: 10000.0
🚀 New in Java 25: Flexible Constructor Bodies (JEP 513)
Starting with Java 25, the rule that super(...) or this(...) must be the first statement in a constructor has been relaxed.
Now you can write statements before calling super(...), as long as you don’t use the partially constructed object (e.g., accessing this fields/methods prematurely).
This makes it easier to perform validation, pre-computations, or initializing subclass fields before delegating to the parent constructor.
👎 Before Java 25
public class Child extends Parent {
public Child(String name) {
super(validate(name)); // validation must be static
}
private static String validate(String name) {
if (name == null) throw new IllegalArgumentException();
return name;
}
}
🚀 From Java 25 Onwards
public class Child extends Parent {
public Child(String name) {
if (name == null) throw new IllegalArgumentException(); // direct validation
super(name); // cleaner & more natural
}
}
✅ Key Benefits
- Perform validation directly inside constructors.
- Initialize subclass fields before calling the parent constructor.
- Write cleaner, more natural, and less error-prone code.
This feature is part of Java 25. You can read more in the official Java 25 Release Notes.
Key Takeaways
superis used to access parent class variables, methods, and constructors.- It helps resolve conflicts when subclass members override parent members.
- In Java 25, you can now write code before
super(...)calls, making constructors much more flexible. superensures code reusability, clarity, and better design in inheritance.
Conclusion
The super keyword in Java is a powerful tool in object-oriented programming. It allows developers to build on existing functionality instead of rewriting everything from scratch. Whether you’re handling constructors, methods, or variables, super ensures smooth interaction between child and parent classes.
