Java super Keyword

In this lesson of Java course, we will learn about the Java super keyword.In Java, the super keyword is used to refer to the parent class or the super class. We will take a look at the different examples and it’s use cases for better understanding.

1. Java Super Keyword

Java super keyword is used to refer to the parent class or the superclass. It can be used in a variety of ways, including to call a superclass constructor or to access superclass methods or variables. It’s important that we understand Java inheritance to have better understanding of the Java super keyword.

2. Uses of Java Super Keyword

Java super is an important keyword and have a lot of use cases in the Java programming. Let’s take a look at some of the main use cases of using the super keyword in Java.

2.1. Superclass constructor

Java call the super class constructor implicitly whenever we create an object of the subclass. There will be many use cases (we will talk them in future lesson), where you want to call the super class constructor explicitly, in all these cases, we can use the super keyword. Let’s take a look at the following example:

public class BMW extends Car {
   //calling construcotr 
   public BMW() {
        super(); // Call the superclass constructor
    }
}
Java super Keyword
Access parent class constructor using super

2.2. Calling super class Methods

Java super keyword is handy when subclass will override method of the super class. We can use the super keyword to access the same method defined in the super class. Let’s take a look at the following example for the understanding:

class Car {

  // method in the superclass
  public void name() {
    System.out.println("Its a generic car");
  }
}

// Audi inherits Car
class Audi extends Car {

  // overriding the name() method
  @Override
  public void name() {
    // call method of superclass
    super.name();
    System.out.println("Welcome to Audi World!!");
  }

In the above example, the Audi class overrides the name() method of its superclass Car. However, it still calls the superclass version of the method using the super.name(). This allows the subclass to reuse the superclass implementation while adding additional functionality. Also the syntax clearly highlight that we are calling the same method from the super class adding more readability to the code.

Calling super class method using super keyword

2.3 Superclass variable using super keyword

In many cases, we want to access the variable defined in the superclass, in all such cases, super is helpful. It provide us the option to access these variables defined in the parent class. This gives us the option to use the variable from parent / super class without overriding it in the subclass.

class Car {
    // method in the superclass
    public void name() {
        System.out.println("Its a generic car");
    }
}

// Audi inherits Car
class Audi extends Car {

    public String cartTpe;
    // overriding the name() method
    @Override
    public void name() {
        // call method of superclass
        super.name();
        System.out.println("Welcome to Audi World!!");
    }

    // new method in subclass
    public void greeting() {
        String cartType = this.type; //Access the superclass variable
        System.out.println("Welcome to Q5.");
    }
}

Summary

In this lesson we learn about the super keyword. The super is a powerful tool which allow you to reuse the code defined in the parent class without overriding it in the child class.It can be used to call superclass constructors, access superclass methods, and variables. A good understanding of the use of super keyword can help building Java code more efficiently. As Always the source code for this course is available on our GitHub repository.

Scroll to Top