Table of contents
1.
Introduction
2.
Characteristics of Super Keyword in Java
3.
Use of super keyword in Java
3.1.
1. Use of super with Variables
3.2.
2. Use of super with Methods
3.3.
3. Use of super with constructors
4.
Advantages of Using Java Super Keyword
5.
Frequently Asked Questions
5.1.
What is the super keyword used for in Java?
5.2.
Can the super keyword be used in a class without a superclass?
5.3.
How does the super keyword affect the reusability of code?
6.
Conclusion
Last Updated: Jul 22, 2024
Easy

Superclass In Java

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, the superclass is a fundamental concept that allows for code reuse & inheritance. It serves as a blueprint for creating subclasses, which can inherit & extend the properties & methods of the superclass. By using the superclass, developers can create a hierarchical structure of classes, making their code more organized, efficient & maintainable. 

Superclass In Java

In this article, we will discuss the characteristics of the super keyword in Java, its various uses with variables, methods & constructors & the advantages it offers in object-oriented programming.

Characteristics of Super Keyword in Java

The super keyword in Java is used to refer to the immediate parent class of a subclass. It allows access to the members (variables, methods & constructors) of the superclass from within the subclass. The super keyword is particularly useful when there are variables or methods with the same name in both the superclass & subclass. By using super, you can explicitly specify which member you want to access or invoke.

For example : 

class Animal {
    protected String name;
    public Animal(String name) {
        this.name = name;
    }
    public void eat() {
        System.out.println("Animal is eating.");
    }
}
class Dog extends Animal {
    private String breed;
    public Dog(String name, String breed) {
        super(name);
        this.breed = breed;
    }
    public void bark() {
        System.out.println("Dog is barking.");
    }
}


In the above code, the `Dog` class extends the `Animal` class. The `Dog` constructor uses the `super` keyword to invoke the constructor of the `Animal` class, passing the `name` parameter. This ensures that the `name` variable in the `Animal` class is properly initialized.

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass