The super keyword in java is a reference variable that is used to refer parent class objects. The keyword “super” came into the picture with the concept of Inheritance. It is majorly used in the following contexts:
1. Use of super with variables: 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. We can understand it more clearly using this code snippet:
Maximum Speed: 120
In the above example, both 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 parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. This code snippet helps to understand the said usage of super keyword.
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 super keyword, message() of superclass could also be invoked.
3. Use of super with constructors: 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 upon the situation. Following is the code snippet to explain the above concept:
Person class Constructor Student class Constructor
In the above example we have called the superclass constructor using keyword ‘super’ via subclass constructor.
Other Important points:
- Call to super() must be first statement in Derived(Student) Class constructor.
- 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. Object does have such a constructor, so if 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 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 you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Access Super Class Methods and Instance Variables Without super Keyword in Java
- Difference between super and super() in Java with Examples
- Using the super Keyword to Call a Base Class Constructor in Java
- super keyword for Method Overloading in Java
- Accessing Grandparent’s member in Java using super
- Difference between super() and this() in java
- super and this keywords in Java
- OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super)
- Comparison of static keyword in C++ and Java
- volatile keyword in Java
- transient keyword in Java
- strictfp keyword in java
- final keyword in java
- abstract keyword in java
- static keyword in java
- return keyword in Java
- native keyword in java
- Protected keyword in Java with examples
- var keyword in Java
- Usage of Enum and Switch keyword in Java
Improved By : vinaydevs00