When we use inheritance, a child class gets access to properties and methods from its parent class. Sometimes, however, we need to specifically refer to something that belongs to the parent class.
This is where the super keyword comes in.
In simple words, super is used to refer to the immediate parent class.
We commonly use it to access parent class variables, call parent class methods, and call parent class constructors.
Accessing a Parent Class Variable
Suppose both the parent and child class have a variable with the same name:
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
void showNames() {
System.out.println(name);
System.out.println(super.name);
}
}Now:
Dog dog = new Dog();
dog.showNames();Output:
Dog
AnimalThe first name refers to the variable in the Dog class.
The super.name refers specifically to the name variable in the parent Animal class.
So you can think of it like:
name → current class
super.name → parent classCalling a Parent Class Method
We can also use super to call a method from the parent class.
For 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 showAnimalSound() {
super.sound();
}
}Now:
Dog dog = new Dog();
dog.sound();
dog.showAnimalSound();Output:
Dog barks.
Animal makes a sound.Normally, dog.sound() calls the overridden method in Dog.
But:
super.sound();specifically calls the parent's version of sound().
Using super with Constructors
One of the most important uses of super is calling the parent class constructor.
Suppose we have:
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog constructor");
}
}Now:
Dog dog = new Dog();Output:
Animal constructor
Dog constructorWhen the Dog object is created, the parent constructor runs first, followed by the child constructor.
The statement:
super();calls the constructor of the parent class.
Passing Arguments to the Parent Constructor
We can also pass arguments to the parent constructor.
For example:
class Animal {
String name;
Animal(String name) {
this.name = name;
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
}Now we can create:
Dog dog = new Dog("Buddy");
System.out.println(dog.name);Output:
BuddyHere:
super(name);passes the name value to the parent Animal constructor.
This is useful when the parent class is responsible for initializing some of the object's data.
super() Is Called Automatically
If the parent class has a no-argument constructor, Java automatically calls it from the child constructor if you don't explicitly write super().
For example:
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog constructor");
}
}This behaves as if we had written:
Dog() {
super();
System.out.println("Dog constructor");
}So the parent constructor runs first.
However, if the parent class only has a parameterized constructor, the child must explicitly call the appropriate parent constructor.
super vs this
Since we've already learned about the this keyword, it's useful to compare the two.
this refers to the current object.
super refers to the immediate parent class.
For example:
this.namerefers to the current object's name.
While:
super.namerefers to the parent class's name.
You can remember it like this:
this→ current class/object
super→ parent class
A Complete Example
Let's put the main ideas together:
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void sound() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
@Override
void sound() {
super.sound();
System.out.println(name + " barks.");
}
}
class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.sound();
}
}Output:
Animal makes a sound.
Buddy barks.Here, super(name) calls the parent constructor, while super.sound() calls the parent's sound() method.
The main idea is simple: when working with inheritance, use super when you specifically need to access something from the parent class.