Use of super keyword in Java
1. Use of super with Variables
When a subclass has a variable with the same name as a variable in its superclass, you can use the super keyword to refer to the superclass variable explicitly. This is particularly useful when you want to access or modify the superclass variable from within the subclass.
Example:
class Vehicle {
protected String brand;
public Vehicle(String brand) {
this.brand = brand;
}
}
class Car extends Vehicle {
private String model;
public Car(String brand, String model) {
super(brand);
this.model = model;
}
public void displayDetails() {
System.out.println("Brand: " + super.brand);
System.out.println("Model: " + model);
}
}
In this example, the `Car` class extends the `Vehicle` class. Both classes have a `brand` variable. By using `super.brand` in the `displayDetails()` method, we explicitly refer to the `brand` variable of the `Vehicle` class, even though the `Car` class has its own `brand` variable.
2. Use of super with Methods
The super keyword can also be used to call methods defined in the superclass from within the subclass. This is useful when you want to invoke the superclass implementation of a method before or after performing some additional operations in the subclass.
Example:
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound.");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
super.makeSound();
System.out.println("Cat meows.");
}
}
In this example, the `Cat` class overrides the `makeSound()` method inherited from the `Animal` class. By calling `super.makeSound()` inside the overridden method, we invoke the superclass implementation of `makeSound()` before printing "Cat meows." This allows us to extend the functionality of the superclass method while still retaining its original behavior.
3. Use of super with constructors
The super keyword is commonly used to call the constructor of the superclass from within the constructor of the subclass. This is important when the superclass has parameterized constructors, and you want to initialize the superclass variables using those constructors.
Example:
class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
class Student extends Person {
private int studentId;
public Student(String name, int studentId) {
super(name);
this.studentId = studentId;
}
}
In this example, the `Student` class extends the `Person` class. The `Person` class has a parameterized constructor that takes a `name` parameter. By using `super(name)` in the `Student` constructor, we call the constructor of the `Person` class and pass the `name` parameter to initialize the `name` variable in the `Person` class. This ensures that the `Student` object is properly initialized with both the `name` and `studentId` variables.
Advantages of Using Java Super Keyword
1. Clarity & Maintainability: Using the `super` keyword makes it clear that method calls and variable accesses are being directed to the superclass. This clarity can make the code easier to understand and maintain, especially in large projects where the relationship between classes can become complex.
2. Avoids Variable Shadowing Issues: In Java, it's common to have variables in a subclass that have the same name as variables in its superclass. The `super` keyword helps avoid confusion by allowing explicit access to the superclass's variables, reducing errors related to variable shadowing.
3. Constructor Chaining: Through the use of `super`, constructors in a subclass can call the constructor of its superclass. This is crucial for constructor chaining, ensuring that each class in the inheritance hierarchy is initialized correctly.
4. Method Overriding Flexibility:When a subclass needs to override a method but also retain the functionality of the superclass's method, `super` can be used to invoke the superclass's version of the method within the overridden method. This adds a layer of flexibility in method behavior customization.
5. Promotes Reusability: The `super` keyword fosters the reuse of code by allowing subclasses to leverage and extend the functionality of their superclass methods. This promotes a cleaner and more efficient coding approach, where duplication is minimized and functionality is maximized.
Frequently Asked Questions
What is the super keyword used for in Java?
The super keyword in Java is used to access methods, variables, and constructors from a class's superclass, facilitating inheritance and method overriding.
Can the super keyword be used in a class without a superclass?
No, the super keyword cannot be used in a class that does not have a superclass, as it specifically refers to superclass components.
How does the super keyword affect the reusability of code?
By allowing subclasses to access and build upon the methods and variables of their superclasses, the super keyword enhances code reusability, making it easier to maintain and extend existing code.
Conclusion
In this article, we have learned about the Java super keyword, understood its role in accessing superclass variables, methods, and constructors. We looked into how it enhances clarity, avoids variable shadowing, supports constructor chaining, offers method overriding flexibility, and promotes code reusability. Learning and understanding these aspects helps in creating more efficient and maintainable Java applications.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.