Super keyword in Java

The super keyword in java is a reference variable which is used to refer immediate parent class object. The keyword “super” came into the picture with the concept of Inheritance. Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

Uses of java super Keyword –

  1. super can be used to refer immediate parent class instance variable.
  2. super can be used to invoke immediate parent class method.
  3. super() can be used to invoke immediate parent class constructor.

1. super can be used to refer immediate parent class instance variable –

We can use super keyword to access the data member or field of parent class. This scenario occurs when a derived class and base class has same data members. In that case there is a possibility of ambiguity for the JVM. It is used if parent class and child class have same fields.

Run Online

/* Base class vehicle */
class Vehicle
{
    int maxSpeed = 120;
}
 
/* sub class Car extending vehicle */
class Car extends Vehicle
{
    int maxSpeed = 180;
 
    void display()
    {
        /* print maxSpeed of base class (vehicle) */
        System.out.println("Maximum Speed: " + super.maxSpeed);
    }
}
 
/* Driver program to test */
class Test
{
    public static void main(String[] args)
    {
        Car small = new Car();
        small.display();
    }
}
Output
Maximum Speed: 120
2. super can be used to invoke immediate parent class method –

This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. In other words, it is used if method is overridden.

Run Online

/* Base class Person */
class Person
{
    void message()
    {
        System.out.println("This is person class");
    }
}
 
/* Subclass Student */
class Student extends Person
{
    void message()
    {
        System.out.println("This is student class");
    }
 
    // Note that display() is only in Student class
    void display()
    {
        // will invoke or call current class message() method
        message();
 
        // will invoke or call parent class message() method
        super.message();
    }
}
 
/* Driver program to test */
class Test
{
    public static void main(String args[])
    {
        Student s = new Student();
 
        // calling display() of Student
        s.display();
    }
}
Output
This is student class
This is person class
3. super() can be used to invoke immediate parent class constructor –

The super keyword can also be used to invoke the parent class constructor. One more important thing is that, ‘’super’ can call both parametric as well as non parametric constructors depending upon the situation.

Run Online

/* superclass Person */
class Person
{
    Person()
    {
        System.out.println("Person class Constructor");
    }
}
 
/* subclass Student extending the Person class */
class Student extends Person
{
    Student()
    {
        // invoke or call parent class constructor
        super();
 
        System.out.println("Student class Constructor");
    }
}
 
/* Driver program to test*/
class Test
{
    public static void main(String[] args)
    {
        Student s = new Student();
    }
}
Output
Person class Constructor
Student class Constructor

Important points:

  1. Call to super() must be first statement in Derived(Student) Class constructor.
  2. If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, you will get a compile-time error.

Leave a Reply

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