Super Keyword in Java
The super keyword in Java is a reference variable that is used to refer to parent class objects. An understanding of Inheritance and Polymorphism is needed in order to understand the Java super keyword. The keyword “super” came into the picture with the concept of Inheritance.
Characteristics of super Keyword in Java
In Java, the super keyword is used to refer to the parent class of a subclass. Here are some of its characteristics:
- super is used to call a superclass constructor: When a subclass is created, its constructor must call the constructor of its parent class. This is done using the super() keyword, which calls the constructor of the parent class.
- super is used to call a superclass method: A subclass can call a method defined in its parent class using the super keyword. This is useful when the subclass wants to invoke the parent class’s implementation of the method in addition to its own.
- super is used to access a superclass field: A subclass can access a field defined in its parent class using the super keyword. This is useful when the subclass wants to reference the parent class’s version of a field.
- super must be the first statement in a constructor: When calling a superclass constructor, the super() statement must be the first statement in the constructor of the subclass.
- super cannot be used in a static context: The super keyword cannot be used in a static context, such as in a static method or a static variable initializer.
- super is not required to call a superclass method: While it is possible to use the super keyword to call a method in the parent class, it is not required. If a method is not overridden in the subclass, then calling it without the super keyword will invoke the parent class’s implementation.
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:
- Use of super with variables
- Use of super with methods
- Use of super with constructors
1. Use of super with Variables
This scenario occurs when a derived class and base class has the same data members. In that case, there is a possibility of ambiguity for the JVM.
We can understand it more clearly using the following example:
Example
Java
// Java code to show use of super // keyword with variables // 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(); } } |
Maximum Speed: 120
In the above example, both the base class and subclass have a member maxSpeed. We could access maxSpeed of 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
Java
// Java program to show use of super 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(); } } |
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
// 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(); } } |
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
Java
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); } } |
false
Advantages of Using Java super keyword
The super keyword in Java provides several advantages in object-oriented programming:
- Enables reuse of code: Using the super keyword allows subclasses to inherit functionality from their parent classes, which promotes the reuse of code and reduces duplication.
- Supports polymorphism: Because subclasses can override methods and access fields from their parent classes using super, polymorphism is possible. This allows for more flexible and extensible code.
- Provides access to parent class behavior: Subclasses can access and use methods and fields defined in their parent classes through the super keyword, which allows them to take advantage of existing behavior without having to reimplement it.
- Allows for customization of behavior: By overriding methods and using super to call the parent implementation, subclasses can customize and extend the behavior of their parent classes.
- Facilitates abstraction and encapsulation: The use of super promotes encapsulation and abstraction by allowing subclasses to focus on their own behavior while relying on the parent class to handle lower-level details.
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 Super Keyword
- 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.

- 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.
This article is contributed by Vishwajeet Srivastava. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Please Login to comment...