The super
keyword in Java is a crucial construct used within a subclass to refer to its immediate parent class (also known as the superclass). It provides a mechanism to explicitly access members (fields and methods) of the superclass that might be hidden or overridden by the subclass, and to invoke superclass constructors.
Understanding the super
Keyword
The super
keyword serves as a direct reference to the parent class of the current object. When working with inheritance, where a subclass extends a superclass, there are scenarios where a subclass might define members (variables or methods) with the same name as those in its superclass. In such cases, the super
keyword becomes indispensable for disambiguating between the subclass's member and the superclass's member.
Why Use super
?
The primary reasons for using the super
keyword include:
- Accessing Overridden Members: When a subclass overrides a method or hides a field from its superclass,
super
allows you to invoke the superclass's version. This is particularly useful when you want to extend the functionality of the superclass method rather than completely replacing it. - Invoking Superclass Constructors: Every subclass constructor implicitly or explicitly calls a superclass constructor.
super()
is used to explicitly call a specific constructor of the parent class.
Common Uses of the super
Keyword
The super
keyword has three main applications within Java programming:
1. Accessing Superclass Methods
When a subclass overrides a method that is defined in its superclass, you can use super.methodName()
to call the overridden method of the superclass from within the subclass.
class Animal {
void makeSound() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void makeSound() {
super.makeSound(); // Calls the makeSound() method of Animal class
System.out.println("Dog barks.");
}
}
// Example Usage:
// Dog myDog = new Dog();
// myDog.makeSound();
// Output:
// Animal makes a sound.
// Dog barks.
2. Accessing Superclass Fields
If a subclass declares a field with the same name as a field in its superclass (this is known as field hiding), super.fieldName
can be used to refer to the superclass's field.
class Vehicle {
String type = "Generic Vehicle";
}
class Car extends Vehicle {
String type = "Sports Car"; // Hides the 'type' field from Vehicle
void displayType() {
System.out.println("Subclass type: " + type); // Refers to Car's type
System.out.println("Superclass type: " + super.type); // Refers to Vehicle's type
}
}
// Example Usage:
// Car myCar = new Car();
// myCar.displayType();
// Output:
// Subclass type: Sports Car
// Superclass type: Generic Vehicle
3. Invoking Superclass Constructors
The super()
call is used to explicitly invoke a constructor of the immediate superclass. This call must be the very first statement in the subclass's constructor. If omitted, the compiler automatically inserts super()
(calling the superclass's no-argument constructor).
class Person {
String name;
Person(String name) {
this.name = name;
System.out.println("Person constructor called for: " + name);
}
}
class Student extends Person {
int studentId;
Student(String name, int studentId) {
super(name); // Calls the Person(String name) constructor
this.studentId = studentId;
System.out.println("Student constructor called for: " + name + " (ID: " + studentId + ")");
}
}
// Example Usage:
// Student s = new Student("Alice", 101);
// Output:
// Person constructor called for: Alice
// Student constructor called for: Alice (ID: 101)
super
vs. this
Keyword
While both super
and this
are keywords used to reference objects within a class, they serve distinct purposes.
Feature | this Keyword |
super Keyword |
---|---|---|
Reference | Refers to the current instance of the class. | Refers to the immediate parent class's instance. |
Usage | Accesses current class members, calls current class constructors. | Accesses superclass members, calls superclass constructors. |
Context | Used within any method or constructor of the current class. | Used within subclass methods or constructors to interact with the superclass. |
Constructor | this() calls another constructor within the same class. |
super() calls a constructor of the superclass. |
For more details on Java keywords and inheritance, you can refer to the official Oracle Java Documentation.