super vs this keyword in Java

In this post, we will learn the difference between super and this keyword in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.

Difference between super vs. this keyword in Java

Feature super this
Usage Used to invoke the constructor of the parent class. Used to invoke the constructor of the same class.
Placement Should be the first statement in the constructor body. Should be the first statement in the constructor body.
Constructor Chain If not explicitly called, the compiler automatically inserts a call to the no-argument constructor of the parent class (if available). If not explicitly called, the compiler automatically inserts a call to the no-argument constructor of the same class (if available).
Explicit Call Can be explicitly called to invoke a specific constructor of the parent class with arguments. Can be explicitly called to invoke a specific constructor of the same class with arguments.
Usage in Overridden Methods Often used in constructors of child classes to initialize parent class members. Used in constructors to call other constructors in the same class to avoid code duplication and maintain constructor chaining.

Example

Let's create an example in Java to demonstrate the usage of super and this keyword. We'll use a simple class hierarchy to illustrate their functionalities. Consider the following code:
class Animal {
    String name;

    Animal(String name) {
        this.name = name;
    }

    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    String breed;

    Dog(String name, String breed) {
        super(name);
        this.breed = breed;
    }

    @Override
    void makeSound() {
        super.makeSound();
        System.out.println("Dog barks");
    }

    void printDetails() {
        System.out.println("Name: " + super.name);
        System.out.println("Breed: " + this.breed);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy", "Labrador");
        dog.makeSound();
        System.out.println("------");
        dog.printDetails();
    }
}

Output:

Animal makes a sound
Dog barks
------
Name: Buddy
Breed: Labrador

Explanation: 

We have a superclass Animal with a constructor that sets the name and a makeSound() method that prints a generic sound for any animal. 

The subclass Dog extends Animal and has an additional instance variable breed. Its constructor calls the superclass constructor using super(name) to set the name property and sets the breed property using this.breed = breed

The makeSound() method in Dog overrides the makeSound() method in Animal, but it also calls the super.makeSound() to print the generic animal sound before adding the dog-specific sound. 

The printDetails() method in Dog demonstrates how to access a name from the superclass using super.name and breed from the current class using this.breed

The output shows that the makeSound() method first calls the superclass method using super.makeSound() to print "Animal makes a sound" and then adds "Dog barks" to the output. 

The printDetails() method prints the name from the superclass (using the super.name) and the breed from the current class (using this.breed).

Download Cheat Sheet:



Comments