Super keyword in Java

In Java, the super keyword is used to refer to the immediate parent class of a subclass. It is used to access the members (methods, variables, and constructors) of the parent class from within the subclass. In this article, we will explore the super keyword and how it is used in Java.

Accessing variables and methods of the parent class

The super keyword can be used to access variables and methods of the parent class from within the subclass. Here is an example:

class Animal {
    String name;
    
    public Animal(String name) {
        this.name = name;
    }

    public void eat() {
        System.out.println(name + " is eating.");
    }
}

class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }

    public void eat() {
         super.eat(); // calls the eat() method of the parent class
        System.out.println(name + " is eating bones.");
       }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");
        dog.eat();
    }
}

In this example, we have a Dog class that extends the Animal class. The Dog class overrides the eat() method of the parent class and adds a new behavior. Inside the eat() method of the Dog class, we use the super keyword to call the eat() method of the parent class. This ensures that the behavior of the parent class is preserved in the subclass. Then, we add the new behavior to the eat() method of the Dog class.

Accessing constructors of the parent class

The super keyword can also be used to access constructors of the parent class from within the subclass. Here is an example:

class Animal {
    String name;
    public Animal(String name) {
        this.name = name;
    }
}

class Dog extends Animal {
    public Dog(String name) {
        super(name); // calls the constructor of the parent class
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");
        System.out.println(dog.name); // prints "Buddy"
    }
}


In this example, we have a Dog class that extends the Animal class. Inside the constructor of the Dog class, we use the super keyword to call the constructor of the parent class. This ensures that the initialization of the parent class is done before the initialization of the subclass. As a result, we can access the name variable of the Animal class from within the Dog class.

When to use the super keyword

The super keyword is typically used in situations where we need to access the members of the parent class from within the subclass. It is commonly used when we want to extend the functionality of a parent class while preserving its original behavior.

For example, if we have a class hierarchy where the parent class defines a set of behaviors and the subclass adds new behaviors, we can use the super keyword to ensure that the behaviors of the parent class are preserved in the subclass.

Conclusion

The super keyword is a useful keyword in Java that allows us to access the members of the parent class from within the subclass. It is commonly used when we want to extend the functionality of a parent class while preserving its original behavior. The super keyword can be used to access variables, methods, and constructors of the parent class.