Usage of the super keyword in Java

Hello code enthusiasts! In this tutorial, we will be learning about the usage of the super keyword in Java with some code examples.

super keyword in Java

Before we dive deep into the usage of the super keyword in Java let’s first understand its origin.  Java is an Object-Oriented Programming(OOP)  language and one of the core concepts in OOP is Inheritance. The super keyword is primarily used in this context. 
To learn about Inheritance refer to this link: https://www.geeksforgeeks.org/inheritance-in-java/

The super keyword is used to reference the parent class’s methods, variables or constructors in the child class. For example, if we have 2 classes, Parent and Child, the Child class extends the Parent we can use the super keyword to access the methods, variables, or constructors of the Parent class from the Child class.

The Child class extends the Parent class allowing it to use the Parent class’s variables, methods, and constructors. The super keyword can only be used when a class extends another class. It enables the child class to reference its immediate parent class’s properties and methods.

We learned above that the keyword is used in three different scenarios: methods, variables, and constructors. Let’s explore each of these cases with examples.

1. Usage of super keyword in Java with Variables

Consider two classes, Mom and Daughter, where the daughter class extends the Mom class. In the Mom class, we have a String variable called Mother with a value value “Lakshmi”. To print the grandmother’s name of the daughter i.e. from the Daughter class, we can use the super keyword to access the Mother variable from the Mom class.

Code:

class Mom{  
    String Mother="Lakshmi"; 
}
class Daughter extends Mom{  
    void print(){  
        System.out.println("Grandmother is"+super.Mother);  
        
        } 
    }  

public class Test {

    public static void main(String[] args) {
        Daughter d=new Daughter();  
        d.print();  
    }

}

Output:

Grandmother is: Lakshmi

In this example, we used super. Mother to access the Mother variable from the Mom class, avoiding the need to create a new variable in the Daughter class.

2. Methods

Now let’s take two classes, Parent and Child, where the Child class extends the Parent class. The Parent class has a method called Doing, which we will override in the Child class. Additionally, the Child class will have a method called Print to demonstrate calling the Parent class method using super.

Code:

class Parent{
    void Doing() {
        System.out.println("Walking");
    }
}
class Child extends Parent {
    void Doing() {
        System.out.println("Eating");
    }
    void Print() {
        System.out.println("The Parent is: ");
        super.Doing();
        System.out.println("The Child is: ");
        Doing();
    }
}
public class Test {

    public static void main(String[] args) {
        Child a=new Child();
        a.Print();
        }
}

Output:

The Parent is:
Walking
The Child is:
Eating

In this example super.Doing() calls the Doing method from the Parent class, demonstrating how to access parent class methods.

3. Constructors

Finally, let’s use the super keyword to invoke the parent class constructor from the child class. We have two classes, Parent and Child where Child extends Parent.

Code:

class Parent{
    Parent(){
        System.out.println("The Parent class Constructor");
    }
}
class Child extends Parent{
    Child(){
        super();
        System.out.println("The Child class Constructor");
    }
}

public class SuperConstructor {

    public static void main(String[] args) {
        Child a=new Child();
    }

}

Output:

The Parent class Constructor
The Child class Constructor

In this example, super() is used in the Child constructor to explicitly call the Parent constructor. To ensure that the parent class’s constructor executes before the child class’s constructor,

In summary, we learned that the super keyword in Java allows a child class to access and utilize its parent class’s variables, methods and constructors effectively.

I hope this tutorial helps you understand the usage of the super keyword in Java!

Leave a Comment

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

Scroll to Top