Java super Keyword (super variable, super method, super constructor)

Java super Keyword: super Variable, super Method, super Constructor (With Examples)

✨ Quick Summary

In this post, you’ll learn what the super keyword means in Java and why it is used. We will cover super variable, super method, and super constructor with beginner-friendly examples.

In our previous posts, we learned about Inheritance, Polymorphism, and the this keyword. Now, let’s learn another keyword that is used mainly in inheritance: super.


1) What is super Keyword in Java?

The super keyword in Java refers to the parent class object.

✅ Simple meaning: super = parent class reference


2) Why Do We Use super Keyword?

We use super keyword mainly for:

  • ✅ To access parent class variables
  • ✅ To call parent class methods
  • ✅ To call parent class constructor

3) super Keyword to Access Parent Class Variable

If parent class and child class have the same variable name, then we use super to access the parent class variable.


class Parent {
    int x = 10;
}

class Child extends Parent {
    int x = 20;

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

public class SuperVariableExample {
    public static void main(String[] args) {

        Child c = new Child();
        c.show();
    }
}
Output:



4) super Keyword to Call Parent Class Method

If child class overrides the parent class method, but you still want to call the parent method, then use super.methodName().


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

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

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

public class SuperMethodExample {
    public static void main(String[] args) {

        Dog d = new Dog();
        d.showSound();
    }
}
Output:


5) super Keyword to Call Parent Class Constructor

You can call the parent class constructor using super().

⚠️ Important Rule: super() must be the first statement inside the constructor.


class Parent {

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

class Child extends Parent {

    Child() {
        super(); // calls parent constructor
        System.out.println("Child constructor called");
    }
}

public class SuperConstructorExample {
    public static void main(String[] args) {

        Child c = new Child();
    }
}
Output:

6) this vs super (Important)

this super
Refers to current class object Refers to parent class object
Used to access current class members Used to access parent class members
Used to call current class constructor Used to call parent class constructor

7) Interview Notes (Very Important)

  • super is used in inheritance
  • ✅ super refers to parent class
  • ✅ Used for variables, methods, constructors
  • ⚠️ super() must be first statement in constructor

Conclusion

The super keyword in Java is used to access parent class members. It plays a very important role in inheritance and also features in most interview questions.

Next Post: Java final Keyword (final variable, final method, final class)

Comments

Post a Comment

Popular posts from this blog

The Java Blueprint: Your First Step into Software Development

Java Variables and Data Types: Storing Your Data

Java Strings (Beginner Guide with Examples)