The super keyword in Java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
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
Use of super with Variables:
We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.
class Vehicle {
int maxSpeed = 130;
}
// 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: 130
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.
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.
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.
Example :
// 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.
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.
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.
Kommentare