Open In App

Super Keyword in Java

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

The Keyword “super” came into the picture with the concept of Inheritance. In this article, we gonna covers all about super in Java including definitions, examples, Uses, Syntax, and more.



Characteristics of Super Keyword in Java

In Java, super keyword is used to refer to the parent class of a subclass. Here are some of its key characteristics:



Overall, the super keyword is a powerful tool for subclassing in Java, allowing subclasses to inherit and build upon the functionality of their parent classes.

Use of super keyword in Java

It is majorly used in the following contexts as mentioned 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 r the JVM

We can understand it more clearly using the following example:

Example




// super keyword in java example
  
// 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
class Test {
    public static void main(String[] args)
    {
        Car small = new Car();
        small.display();
    }
}

Output
Maximum Speed: 120

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.

This code snippet helps to understand the said usage of the super keyword.

Example




// super keyword in java example
  
// 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

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. 

Following is the code snippet to explain the above concept:

Example 1




// Java Code to show 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

In the above example, we have called the superclass constructor using the keyword ‘super’ via subclass constructor.

Example 2




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();
  
        // prints "false"
        System.out.println(result);
    }
}

Output
false

Advantages of Using Java Super Keyword

The super keyword in Java provides many advantages in object-oriented programming are as follows:

Overall, 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 to Remember While Using “Java Super Keyword”

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

FAQs – Java super Keyword

Q1. What is super () and super keyword in Java?

Super() is a Java keyword used to call a superclass constructor. Super accesses superclass members and maintains inheritance hierarchies.

Q2. Which is the super class of Java?

The Object class aka super class is at the top of the class hierarchy in Java’s java.lang package. Every class, whether predefined or user-defined, is a subclass of the Object class.

Q3. Why is Super important in Java?

super is essential in Java as it facilitates the access, initialization, and management of relationships between superclasses and subclasses, thereby promoting code reusability.


Article Tags :