Launch your tech mastery with us—your coding journey starts now!
Course Content
Array Handling
0/1
String Handling
0/1
Wrapper Classes
0/1
Collections in Java
0/1
Packages
0/1
File Handling
0/1
Multithreading
0/1
Java Networking
0/1
Core Java

 

🔹 super Keyword in Java – The Bridge Between Child and Parent 🚀

In Java, the super keyword is like a respectful child calling their parent for help.
It allows a subclass (child) to directly access the parent class (superclass) methods, variables, and constructors.

👉 Think of it like saying:
“Hey Dad, I’ll take care of my work, but if I need your help, I’ll call you using super!” 👨‍👦


✨ Why Do We Need super?

In inheritance, sometimes the child class has the same variables or methods as the parent.
Without super, the child’s version hides the parent’s version.

That’s where super steps in — to access the hidden parent features.


🛠 Uses of super Keyword

1️⃣ Access Parent Class Variables

When a subclass has a variable with the same name as the parent, super helps access the parent variable.

class Animal {
    String name = "Animal";
}

class Dog extends Animal {
    String name = "Dog";

    void display() {
        System.out.println("Child name: " + name);
        System.out.println("Parent name: " + super.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.display();
    }
}

✅ Output:

Child name: Dog
Parent name: Animal

2️⃣ Access Parent Class Methods

If a method is overridden in the child class, super lets us call the parent’s version of the method.

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

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }

    void showSounds() {
        super.sound();  // Call parent method
        sound();        // Call child method
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.showSounds();
    }
}

✅ Output:

Animal makes a sound
Dog barks

3️⃣ Call Parent Class Constructor

The first statement in a child constructor can be super(), which calls the parent class constructor.

class Animal {
    Animal() {
        System.out.println("Animal constructor called");
    }
}

class Dog extends Animal {
    Dog() {
        super(); // Calls parent constructor
        System.out.println("Dog constructor called");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
    }
}

✅ Output:

Animal constructor called
Dog constructor called

🚀 Quick Recap

  • super.variable → Access parent class variable.

  • super.method() → Call parent class method.

  • super() → Invoke parent class constructor.


🎯 Why Learn super?

✔ Helps resolve naming conflicts between parent and child.
✔ Allows method reusability without rewriting parent code.
✔ Makes constructor chaining possible.

💡 Interview Tip:
👉 Can super() and this() be used together in a constructor?
❌ No! They must always be the first statement, so you can only use one.