Back to the 2020 paper

Module III: Basics of Web Programming

20202m

Which of the following keywords can be used in a subclass to call the constructor of superclass?

(i) Super
(ii) This
(iii) Extent
(iv) Extends

Worked SolutionAI Assisted

Correct Answer: (i) Super (specifically super())

Explanation:

  • In Java, the super keyword is a reference variable used to refer to immediate parent (superclass) class objects.
  • When used with parentheses as super() or super(arguments), it explicitly invokes the constructor of the superclass.
  • Rule: The call to super() must be the very first statement inside the subclass constructor.

Example:

class Parent {
    Parent(String message) {
        System.out.println("Parent Constructor: " + message);
    }
}

class Child extends Parent {
    Child() {
        super("Invoking Superclass Constructor"); // Calls Parent constructor
        System.out.println("Child Constructor");
    }
}

Contrast with other keywords:

  • this(): Invokes another constructor within the same class.
  • extends: Declares class inheritance (class Child extends Parent).

Similar questions