Using the super Keyword to Call a Base Class Constructor in Java


In Java's object-oriented paradigm, inheritance plays a significant role, allowing developers to create classes that reuse, extend, and modify the behavior defined in other classes. To facilitate seamless interaction between a superclass and its subclass, Java provides the super keyword. This article will focus on understanding and effectively using the super keyword in Java to call a base class constructor

Exploring the Super Keyword

The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create an instance of a subclass, an instance of the parent class is also created, which is referenced by the super keyword.

The super keyword serves three main purposes −

  • It can be used to refer to the immediate parent class instance variable.

  • It can be used to invoke the immediate parent class method.

  • It can be used to invoke the immediate parent class constructor

In this article, we'll delve into the third use: invoking a parent class constructor.

Invoking Base Class Constructors with Super

In Java, a constructor is a block of code that initializes the newly created object. A subclass inherits fields and methods from the superclass, but it cannot inherit the constructor. However, the subclass constructor can call the superclass constructor using the super keyword.

When creating an object of the subclass, the constructor of the superclass executes first, and then the constructor of the subclass. If the subclass constructor does not explicitly call the superclass constructor, the Java compiler automatically inserts a no-argument super() call. However, if the superclass does not have a no-argument constructor, the subclass must explicitly call one of the superclass constructors, using super.

Here's a simple example

class Superclass {
   int age;
   Superclass(int age) {
      this.age = age;
   }
}
class Subclass extends Superclass {
   Subclass(int age) {
      super(age);
   }
}

In this example, Subclass calls the constructor of Superclass using super(age). This call must be the first statement in the Subclass constructor.

Understanding the Importance of Calling Superclass Constructors

Calling the superclass constructor is crucial in many cases. For instance, it allows the subclass to inherit and initialize state from the superclass. It also ensures that any initialization logic defined in the superclass constructor gets executed. This is especially important if the superclass is designed to work with subclasses or implements a "template method" design pattern.

Things to Remember when Using Super

While using the super keyword in Java, keep in mind the following points −

  • First statement − The call to super() must be the first statement in each constructor

  • Automatic call − If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

  • Parameter matching − The parameters passed to super() must match the type and order of the parameters in one of the superclass constructors.

Conclusion

The super keyword in Java provides a powerful means of enhancing and controlling the behavior inherited from a superclass. By using super to call a base class constructor, you ensure that your subclasses are correctly initialized and that they fully benefit from the code defined in the superclass.

Updated on: 19-Jul-2023

402 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements