Open In App

Super Keyword in Java

Last Updated : 17 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The super keyword in Java is a reference variable that is used to refer to the parent class when we are working with objects. You need to know the basics of Inheritance and Polymorphism to understand the Java super keyword. 

The Keyword "super" came into the picture with the concept of Inheritance. In this article, we are going to cover all about super keyword in Java, including definitions, examples, uses, syntax, and more.

Characteristics of Super Keyword in Java

The characteristics of the super keyword are listed below:

  • Calling the Parent Class Constructor: When we create an object of the subclass, its constructor needs to call the constructor of the parent class. This can be done with the help of the super keyword, and it calls the constructor of the parent class.
  • Accessing Parent Class Methods: If the subclass wants to access the methods of the parent class, it can also be done with the help of the super keyword.
  • Accessing Parent Class Fields: Fields from the parent class can also be accessed using the super keyword in the subclass.
  • First Statement in a Constructor: When calling a superclass constructor, the super() statement must be the first statement in the constructor of the subclass. This ensures that the parent class is properly initialized before the subclass does anything else.
  • Cannot be used in Static Context: We cannot use super in a static variable, static method and static block.
  • Not Always Required: We know that the super keyword is used to call the methods from the parent class. If a method is not overridden in the subclass, then calling it without the super keyword will invoke the parent class's implementation.

Note: The super keyword is a powerful tool. It allows subclasses to inherit the behavior and functionality of the parent class.

Use of super Keyword in Java

Super Keyword are mainly used in the following contexts which are listed below:

1. Use of super with Variables

This scenario occurs when a derived class and base class have the same data members. In that case, there is a possibility of ambiguity for the JVM

Real-world example: Suppose there is a chile, whose name is "Max" and the child has also a parent named "Max". Normally, to refer to the parent, we would say "parent Max", this is similar to using super.maxSpeed.

Program:

Java
// Demonstrating the use of
// super keyword with variable 

// Base class vehicle
class Vehicle {
    int maxSpeed = 120;
}

// sub class Car extending vehicle
class Car extends Vehicle {
    int maxSpeed = 180;

    void display()
    {
        // print maxSpeed from the vehicle class 
        // using super
        System.out.println("Maximum Speed: "
                           + super.maxSpeed);
    }
}

// Driver Program
class Test {
    public static void main(String[] args)
    {
        Car small = new Car();
        small.display();
    }
}

Output
Maximum Speed: 120

Explanation: In the above example, both the base class and subclass have a member maxSpeed. We could access the maxSpeed of the base class in subclass using super keyword.

2. Use of super with Methods

This is used when we want to call the parent class method. So, whenever a parent and child class have the same-named methods then to resolve ambiguity we use the super keyword.

Real-world Example: It is simply just like when we want to listen to our parents' advice instead of our own decision, super.methodName() helps us follow the parents' behavior in code.

Program:

Java
// Demonstrating the use of super keyword 
// with methods

// superclass Person
class Person {
    void message()
    {
        System.out.println("This is person class\n");
    }
}

// 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
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

Explanation: In the above example, we have seen that if we only call method message() then, the current class message() is invoked but with the use of the super keyword, message() of the superclass could also be invoked.

3. Use of super with Constructors

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

Real-world Example: Before a child born, first the parent exists. Similarly, the parent class constructor must be called before the child's constructor finishes it's work.

Program:

Java
// Demonstrating the use of 
// super keyword with constructor

// 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
class Test {
    public static void main(String[] args)
    {
        Student s = new Student();
    }
}

Output
Person class Constructor
Student class Constructor

Explanation: In the above example, we have called the superclass constructor using the keyword "super" via subclass constructor.

Example:

Java
// Demonstrating how to modify 
// parent methods result

class ParentClass {
    public boolean isTrue() { return true; }
}

class ChildClass extends ParentClass {
    public boolean isTrue()
    {
        // calls parent implementation of isTrue()
        boolean parentResult = super.isTrue();
        // negates the parent result
        return !parentResult;
    }
}

public class Main {
    public static void main(String[] args)
    {
        ChildClass child = new ChildClass();
        
        // calls child implementation
        // of isTrue()
        boolean result = child.isTrue();

        System.out.println(result);
    }
}

Output
false

Explanation: In the above example, the child class changes the behavior of the parent class isTrue() method. It calls the parent's method using super keyword and changes the result, that's why the result is false.

Advantages of Using Java Super Keyword

The advantages of super keyword are listed below:

  • With the help of super keyword, subclasses can inherit the functionality from their parent classes.
  • Subclasses can override methods and can access fields and methods from their parent class with the help of super keyword, because of this the code becomes more flexible.
  • With the help of super keyword we can easily access the methods and fields from the parent class without recreating it in the subclass.
  • With the help of super keyword we can achieve abstraction and encapsulation. Subclass can focus on their specified tasks and the parent class take care of the general functionality.

Note: The super keyword is a key feature of inheritance and polymorphism in Java, and it provides several benefits for developers seeking to write reusable, extensible, and well-organized code.

Important Points:

Here are some Important points that you need to take care of during using super keywords in Java:

  • Call to super() must be the first statement in the Derived(Student) Class constructor because if you think about it, it makes sense that the superclass has no knowledge of any subclass, so any initialization it needs to perform is separate from and possibly prerequisite to any initialization performed by the subclass. Therefore, it needs to complete its execution first.
  • 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. The object does have such a constructor, so if the Object is the only superclass, there is no problem.

super Keyword in Java

  • If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that a whole chain of constructors is called, all the way back to the constructor of Object. This, in fact, is the case. It is called constructor chaining.

Next Article
Article Tags :
Practice Tags :

Similar Reads