Java Intermediate

0% completed

Previous
Next
Super Keyword

The super keyword in Java is a reference variable used to refer to the immediate parent class object. It serves multiple purposes in inheritance, allowing subclasses to access superclass members (variables and methods) and constructors. Understanding how to effectively use super is essential for leveraging the full power of inheritance in Java.

Uses of the super Keyword

Image
  1. Calling the Superclass Constructor:
    • Used to invoke the constructor of the parent class.
    • Must be the first statement in the subclass constructor.
  2. Accessing Superclass Methods:
    • Used to call methods defined in the superclass that may be overridden in the subclass.
  3. Accessing Superclass Variables:
    • Used to refer to variables defined in the superclass that are hidden by variables in the subclass.

Example 1: Using super to Call the Superclass Constructor and Access Superclass Methods

In this example, we'll demonstrate how the super keyword is used to call the superclass constructor and access a superclass method from the subclass.

Java
Java

. . . .

Explanation:

  1. Superclass (Vehicle) Components:

    • Variables: brand, model
      These represent general characteristics common to all vehicles.
    • Constructor:
      Initializes the brand and model of the vehicle.
    • Method: displayInfo()
      Displays the vehicle's brand and model.
  2. Subclass (Car) Components:

    • Inheritance: extends Vehicle
      The Car class inherits from the Vehicle class, acquiring its variables and methods.
    • Constructor:
      • super(brand, model);
        Calls the superclass (Vehicle) constructor to initialize inherited variables.
      • Initializes the subclass-specific variable carType.
    • Method: displayCarType()
      • Displays the car's type.
      • super.displayInfo();
        Calls the superclass (Vehicle) method displayInfo() to display inherited information.

Example 2: Using super to Access Superclass Variables

In this example, we'll demonstrate how the super keyword is used to access superclass variables that may be hidden by subclass variables.

Java
Java

. . . .

Explanation:

  1. Subclass (Dog) Components:
    • Inheritance: extends Animal
      The Dog class inherits from the Animal class, acquiring its variable and method.
    • Constructor:
      • super(name);
        Calls the superclass (Animal) constructor to initialize the inherited name variable.
      • this.name = "Doggo " + name;
        Initializes the subclass-specific name variable, differentiating it from the superclass variable.
      • Initializes the subclass-specific variable breed.
    • Method: displayBreed()
      • Displays the dog's breed.
      • this.name
        Refers to the subclass's name variable.
      • super.name
        Refers to the superclass's name variable, allowing access to the hidden variable.

The super keyword in Java plays a crucial role in inheritance by allowing subclasses to interact seamlessly with their superclasses. Whether it's calling the superclass constructor to initialize inherited variables, accessing superclass methods to reuse functionality, or referring to superclass variables that may be hidden by subclass variables, super provides the necessary tools to maintain a clean and efficient inheritance structure.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next