The super
keyword is a reference variable used to refer to the immediate parent class object. It is primarily used to access methods, constructors, and variables of the parent class from the subclass.
Important Uses of ‘super’ keyword:
-
Access Parent Class Constructor: The
super()
is used to call the constructor of the parent class. -
Access Parent Class Method: The
super
keyword is used to invoke a parent class method that has been overridden. -
Access Parent Class Variable: You can use
super
to access a parent class variable when there is a variable with the same name in the subclass.
Syntax:
-
Calling the parent class constructor:
super(); // Calls the no-argument constructor of the parent class
super(arg1, arg2); // Calls the parent class constructor with arguments
Code language: JavaScript (javascript)
- Accessing the parent class method:
super.methodName(); // Calls the parent class method
Code language: JavaScript (javascript)
- Accessing the parent class variable:
super.variableName; // Access parent class variable
Code language: JavaScript (javascript)
Example: Software Engineer Hierarchy
Let’s consider a software engineer hierarchy where the Employee
class is the parent class, and the SoftwareEngineer
class extends it. The Employee
class has a constructor, a method to print the name, and a field for the name. The SoftwareEngineer
class extends Employee
and adds more specific functionality like the specialization
.
class Employee { String name; // Constructor of the parent class public Employee(String name) { this.name = name; } // Method to display the name of the employee public void showDetails() { System.out.println("Employee Name: " + name); } } class SoftwareEngineer extends Employee { String specialization; // Constructor of the child class public SoftwareEngineer(String name, String specialization) { super(name); // Calling the constructor of the parent class to initialize the name this.specialization = specialization; } // Overriding the showDetails method @Override public void showDetails() { super.showDetails(); // Calling the parent class method to display the name System.out.println("Specialization: " + specialization); // Adding new functionality for the subclass } // A method to display both the employee name and specialization public void displayFullDetails() { // Accessing superclass member variable using super System.out.println("Employee Name (from super): " + super.name); // Accessing name from superclass System.out.println("Specialization: " + this.specialization); // Accessing specialization from subclass } } public class Main { public static void main(String[] args) { // Creating an instance of SoftwareEngineer, which inherits from Employee SoftwareEngineer engineer = new SoftwareEngineer("Mahesh", "Backend Development"); // Calling the overridden method from the subclass engineer.showDetails(); // Will show employee name and specialization // Calling a method that uses super to access superclass members engineer.displayFullDetails(); // Will access both superclass and subclass members } } /* Employee Name: Mahesh Specialization: Backend Development Employee Name (from super): Mahesh Specialization: Backend Development */
The super
keyword in Java is an essential tool for dealing with inheritance. It serves as a bridge between the subclass and the parent class, allowing a subclass to access and invoke methods, constructors, and variables defined in its superclass. Here’s a summary of key points:
-
Calling Superclass Constructor:
-
The
super()
keyword is used to invoke the constructor of the superclass, which is necessary when a subclass needs to initialize inherited fields or ensure that the parent class is correctly initialized before the subclass. -
This is especially useful when the superclass has a constructor that requires parameters.
-
-
Accessing Superclass Methods:
-
The
super.methodName()
syntax allows a subclass to invoke a method that exists in the superclass. This is helpful if the subclass has overridden the method and you still want to call the original method from the superclass. -
You can also override a method in the subclass and call the parent class’s method within the overridden version to extend the functionality.
-
-
Accessing Superclass Variables:
-
The
super.variableName
syntax is used to access variables in the superclass. This is helpful if a subclass has a variable with the same name and you need to reference the one in the parent class.
-
-
Inheritance and Polymorphism:
-
By using
super
, subclasses can maintain proper communication with the parent class, leveraging polymorphism while avoiding conflicts in variable names and method implementations.
-
In practice, super
promotes cleaner, more maintainable code by enabling subclasses to reuse functionality from parent classes without duplication. It is crucial for any Java developer to understand and apply super
correctly, especially in the context of inheritance, to write effective and efficient object-oriented programs.