Which of this keyword can be used in subclass to call the constructor of super class?

9.2 Calling the Superclass Constructor

A subclass can have its own private data members, so a subclass can also have its own constructors.

The constructors of the subclass can initialize only the instance variables of the subclass. Thus, when a subclass object is instantiated the subclass object must also automatically execute one of the constructors of the superclass.

To call a superclass constructor the super keyword is used. The following example programs demonstrate use of super keyword.

Which of this keyword can be used in subclass to call the constructor of super class?

(Rectangle.java)

/**
 *  This class holds data of a Rectangle.
 */

public class Rectangle
{
   private double length;  // To hold length of rectangle
   private double width;  // To hold width of rectangle

   /**
    *  The constructor initialize rectangle's 
    *  length and width with default value    
    */

   public Rectangle()
   {
      length = 0;
      width = 0;
   }

   /**
    *  The constructor accepts the rectangle's  
    *  length and width.   
    */

   public Rectangle(double length, double width)
   {
      this.length = length;
      this.width = width;
   }

   /**
    *  The getArea method returns the area of 
    *  the rectangle.
    */

   public double getArea()
   {
      return length * width;
   }
}

(Box.java)

/**
 *  This class holds data of a Box.
 */

public class Box extends Rectangle
{
   private double height;  // To hold height of the box

   /**
    *  The constructor initialize box's 
    *  length, width and height with default value.  
    */

   public Box()
   {
      // Call the superclass default constructor to
      // initialize length and width.
      super();
      //Initialize height.
      height = 0;
   }

   /**
    *  The constructor accepts the box's  
    *  length, width and height.   
    */

    public Box(double length, double width, double height)
    {
      // Call the superclass constructor to
      // initialize length and width.
      super(length, width);
      
      // Initialize height.
      this.height = height;
   }

   /**
    *  The getVolume method returns the volume of 
    *  the box.
    */

   public double getVolume()
   {
      return getArea() * height;
   }
}

(BoxDemo.java)

/**
 * This program demonstrates calling
 * of superclass constructor.
 */

public class BoxDemo
{
   public static void main(String[] args)
   {
      // Create a box object.
      Box myBox1 = new Box();

      // Display the volume of myBox1.
      System.out.println("Volume: " + myBox1.getVolume());

      // Create a box object.
      Box myBox2 = new Box(12.2, 3.5, 2.0);

      // Display the volume of myBox2.
      System.out.println("Volume: " + myBox1.getVolume());
   }
}

The super keyword in java is a reference variable that is used to refer to parent class objects. An understanding of Inheritance and Polymorphism is needed in order to understand the super keyword. The keyword “super” came into the picture with the concept of Inheritance. It is majorly used in the following contexts:

  • Use of super with variables
  • Use of super with methods
  • Use of super with constructors

1. Use of super with variables

This scenario occurs when a derived class and base class has the same data members. In that case, there is a possibility of ambiguity for the JVM. We can understand it more clearly using this code snippet: 

Java

class Vehicle {

    int maxSpeed = 120;

}

class Car extends Vehicle {

    int maxSpeed = 180;

    void display()

    {

        System.out.println("Maximum Speed: "

                           + super.maxSpeed);

    }

}

class Test {

    public static void main(String[] args)

    {

        Car small = new Car();

        small.display();

    }

}

In the above example, both base class and subclass have a member maxSpeed. We could access maxSpeed of base class in subclass using super keyword.

2. Use of super with methods

This is used when we want to call the parent class method. So whenever a parent and child class have the same-named methods then to resolve ambiguity we use the super keyword. This code snippet helps to understand the said usage of the super keyword.

Java

class Person {

    void message()

    {

        System.out.println("This is person class\n");

    }

}

class Student extends Person {

    void message()

    {

        System.out.println("This is student class");

    }

    void display()

    {

        message();

        super.message();

    }

}

class Test {

    public static void main(String args[])

    {

        Student s = new Student();

        s.display();

    }

}

Output

This is student class
This is person class

In the above example, we have seen that if we only call method message() then, the current class message() is invoked but with the use of the super keyword, message() of superclass could also be invoked.

3. Use of super with constructors

The super keyword can also be used to access the parent class constructor. One more important thing is that ‘super’ can call both parametric as well as non-parametric constructors depending upon the situation. Following is the code snippet to explain the above concept: 

Java

class Person {

    Person()

    {

        System.out.println("Person class Constructor");

    }

}

class Student extends Person {

    Student()

    {

        super();

        System.out.println("Student class Constructor");

    }

}

class Test {

    public static void main(String[] args)

    {

        Student s = new Student();

    }

}

Output

Person class Constructor
Student class Constructor

In the above example, we have called the superclass constructor using the keyword ‘super’ via subclass constructor.

Important Points to Remember while using Super Keyword

  • Call to super() must be the first statement in the Derived(Student) Class constructor because if you think about it, it makes sense that the superclass has no knowledge of any subclass, so any initialization it needs to perform is separate from and possibly prerequisite to any initialization performed by the subclass. Therefore, it needs to complete its execution first.
  • 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. If the superclass does not have a no-argument constructor, you will get a compile-time error. The object does have such a constructor, so if the Object is the only superclass, there is no problem.

Which of this keyword can be used in subclass to call the constructor of super class?

  • If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that a whole chain of constructors is called, all the way back to the constructor of Object. This, in fact, is the case. It is called constructor chaining.

This article is contributed by Vishwajeet Srivastava. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


Which keyword is used in subclass to call the constructor of superclass?

The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor.

Which of this keyword can be used in a subclass to call the constructor of super class 1 point super this extent extends?

1. Which of this keyword can be used in a subclass to call the constructor of superclass? Explanation: None.

Which of this keyword can be used in a subclass to call the constructor of superclass * super this super () extends?

super keyword super is used to refer super-class's instance as well as static members. super is also used to invoke super-class's method or constructor.

Which of this keyword can be used in a subclass to call the constructor of superclass * 1 point?

Super class's Constructor in inheritance In inheritance constructors are not inherited. You need to call them explicitly using the super keyword.