5-πŸ”‘ super Keyword in Java – Complete Guide with Examples, Constructor Calls & Common Mistakes

🌟 Introduction

In the previous articles, we explored:

  • Inheritance
  • Types of Inheritance
  • Method Overriding
  • Parent Reference & Child Object

Now comes a very important concept that connects all of these together:

πŸ‘‰ The super keyword

When we use inheritance, a child class inherits properties and methods from the parent class. But sometimes, the child class:

  • Has variables with the same name as the parent
  • Overrides parent methods
  • Needs to call the parent constructor

In all these cases, Java provides a special keyword:

πŸ‘‰ super

Understanding super is essential because it helps us control and access parent class behavior, especially when overriding happens.


🧠 What is super Keyword?

πŸ‘‰ super is a reference variable in Java that is used to refer to the immediate parent class object.

In simple words:

super allows a child class to access the parent class members.


🎯 Where is super Used?

The super keyword is mainly used in three ways:

1️⃣ To access parent class variables
2️⃣ To call parent class methods
3️⃣ To call parent class constructors

Let’s understand each one in detail.


πŸ”Ή 1. Accessing Parent Class Variable Using super


πŸ’» Example

class Animal {

    String name = "Animal";

}

class Dog extends Animal {

    String name = "Dog";

    void printName() {
        System.out.println(name);
        System.out.println(super.name);
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();
        d.printName();

    }

}

πŸ”Ž Explanation

String name = "Animal";

Parent class variable


String name = "Dog";

Child class variable (same name)


System.out.println(name);

πŸ‘‰ Refers to current class variable (Dog)


System.out.println(super.name);

πŸ‘‰ Refers to parent class variable (Animal)


🧾 Output

Dog
Animal

⚠️ Important Concept

πŸ‘‰ If both parent and child have same variable names:

  • this.name β†’ child class
  • super.name β†’ parent class

πŸ”Ή 2. Calling Parent Class Method Using super


πŸ’» Example

class Animal {

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

}

class Dog extends Animal {

    void sound() {
        super.sound();
        System.out.println("Dog barks");
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();
        d.sound();

    }

}

πŸ”Ž Explanation

Child method:

void sound()

Overrides parent method


super.sound();

πŸ‘‰ Calls parent class version of method


🧾 Output

Animal makes sound
Dog barks

πŸ”₯ Key Insight

πŸ‘‰ Without super, parent method would not be called
πŸ‘‰ With super, both parent + child behavior can be combined


πŸ”Ή 3. Calling Parent Class Constructor Using super()


πŸ’» Example

class Animal {

    Animal() {
        System.out.println("Animal Constructor");
    }

}

class Dog extends Animal {

    Dog() {
        super();
        System.out.println("Dog Constructor");
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();

    }

}

πŸ”Ž Explanation

super();

πŸ‘‰ Calls parent class constructor


🧾 Output

Animal Constructor
Dog Constructor

⚠️ Important Rule

πŸ‘‰ super() must be the first statement in constructor


🧠 Hidden Behavior

Even if you don’t write:

super();

πŸ‘‰ Java automatically inserts it (default constructor case)


βš™οΈ Rules of super Keyword


βœ” Rule 1: Refers to Immediate Parent

super can only access direct parent, not grandparent.


βœ” Rule 2: Cannot Be Used in Static Context

static void method() {
// super not allowed here ❌
}

βœ” Rule 3: Must Be First Statement in Constructor

Dog() {
super(); // must be first
}

βœ” Rule 4: Used Only Inside Child Class

super is meaningful only in inheritance


πŸ”„ this vs super (Very Important)

Featurethissuper
Refers toCurrent classParent class
AccessOwn membersParent members
Constructorthis()super()

🧨 Tricky Interview Example

class A {

    int x = 10;

}

class B extends A {

    int x = 20;

    void show() {
        System.out.println(x);
        System.out.println(super.x);
    }

}

public class Main {

    public static void main(String[] args) {

        B obj = new B();
        obj.show();

    }

}

🧾 Output

20
10

⚠️ Common Mistakes & Confusions


❌ Mistake 1: Forgetting super() position

Dog() {
System.out.println("Dog");
super(); // ❌ Error
}

❌ Mistake 2: Confusing this and super

πŸ‘‰ this β†’ current class
πŸ‘‰ super β†’ parent class


❌ Mistake 3: Using in static method

static void test() {
super.sound(); // ❌ Not allowed
}

❌ Mistake 4: Thinking super accesses all ancestors

❌ Only immediate parent


🎯 Real-Life Analogy

Think:

Child β†’ refers to Parent using "super"

Just like:

πŸ‘‰ You calling your parent for help


🧠 Key Points to Remember

βœ” super refers to parent class object
βœ” Used for variables, methods, constructors
βœ” Helps resolve overriding conflicts
βœ” Must be first in constructor
βœ” Cannot be used in static context


🏁 Conclusion

The super keyword is a powerful feature in Java that allows a child class to access and interact with its parent class.

It plays a crucial role in:

  • Method overriding
  • Constructor chaining
  • Resolving variable conflicts

In this article, we learned:

  • What super is
  • How to access parent variables
  • How to call parent methods
  • How constructors work with super()
  • Common mistakes and interview traps

Mastering super helps you understand how inheritance works internally, making your code more structured and maintainable.


Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *