Super Keyword in Java

In Java, the super keyword plays a crucial role in inheritance and provides a way to refer to the superclass members (fields, methods, and constructors) from a subclass. It allows us to access and invoke superclass functionality, facilitating code reuse and overriding.

  1. Accessing Superclass Members:
    The primary usage of the super keyword is to access and invoke superclass members, including fields and methods. By using super, we can explicitly refer to the superclass version of a member and avoid name clashes or provide access to overridden methods.

Example:

Java
class Animal {
    String name = "Animal";

    void makeSound() {
        System.out.println("Animal is making a sound.");
    }
}

class Dog extends Animal {
    String name = "Dog";

    @Override
    void makeSound() {
        super.makeSound(); // Invoke superclass method
        System.out.println("Dog is barking.");
    }

    void printNames() {
        System.out.println(super.name); // Access superclass field
        System.out.println(this.name); // Access subclass field
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound();  // Output: Animal is making a sound. Dog is barking.
        dog.printNames(); // Output: Animal Dog
    }
}

Explanation:
In the above example, the Dog class extends the Animal class. The makeSound() method in the Dog class overrides the makeSound() method in the Animal class. By using super.makeSound(), we can invoke the superclass method and add additional behavior specific to the Dog class.

The printNames() method demonstrates accessing the superclass field name using super.name and the subclass field name using this.name.

  1. Invoking Superclass Constructors:
    Another use of the super keyword is to invoke superclass constructors. When a subclass is created, the superclass constructor is automatically called. However, in certain scenarios, we may need to explicitly call a specific superclass constructor using super().

Example:

Java
class Vehicle {
    String type;

    Vehicle(String type) {
        this.type = type;
    }
}

class Car extends Vehicle {
    int wheels;

    Car(String type, int wheels) {
        super(type); // Invoke superclass constructor
        this.wheels = wheels;
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car("Sedan", 4);
        System.out.println("Type: " + car.type);     // Output: Type: Sedan
        System.out.println("Wheels: " + car.wheels); // Output: Wheels: 4
    }
}

Explanation:
In the above example, the Car class extends the Vehicle class. The Car class has an additional field wheels. By using super(type), we invoke the superclass constructor to initialize the type field. This ensures that the type field is properly set when creating a Car object.

  1. Superclass Method Overriding:
    The super keyword is also used to invoke overridden superclass methods from a subclass. When a method is overridden in a subclass, using super.methodName() allows us to call the superclass version of the method.

Example:

Java
class Shape {
    void display() {
        System.out.println("Shape");
    }
}

class Rectangle extends Shape {
    @Override
    void display() {


        super.display(); // Invoke superclass method
        System.out.println("Rectangle");
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        rectangle.display(); // Output: Shape Rectangle
    }
}

Explanation:
In this example, the Rectangle class extends the Shape class and overrides the display() method. By using super.display(), we invoke the display() method of the Shape class, and then add the specific behavior of the Rectangle class.

The super keyword is a powerful tool in Java that enables us to interact with and utilize superclass members, invoke superclass constructors, and call overridden superclass methods. Understanding and properly using the super keyword is essential in object-oriented programming and inheritance scenarios, allowing for code reuse and extensibility. By leveraging the super keyword effectively, developers can harness the full potential of inheritance in Java and build robust, modular codebases.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top