Which of this keyword can be used in a subclass to call the constructor of parent class *?

When we inherit class into another class then object of base class is initialized first. If a class do not have any constructor then default constructor will be called. But if we have created any parameterized constructor then we have to initialize base class constructor from derived class.

We have to call constructor from another constructor. It is also known as constructor chaining.

When we have to call same class constructor from another constructor then we use this keyword. In addition, when we have to call base class constructor from derived class then we use base keyword.

In the following example we have created Abc is as a base class and inherit it into Pqr. We have created parameterized constructor in class Abc so we have to initialize this constructor from derived class constructor in the following manner:-



		
public class Abc
{
    public int p, q;
    public Abc(int p1, int p2)
    {
        p = p1;
        q = p2;
    }
    public int sum(int x, int y)
    {
        return (x + y);
    }
    
}

//derived class/ child class
public class Pqr : Abc
{
    public int a;
    public Pqr(int a1,int p1, int p2):base(p1,p2)
    {
        a = a1;
    }
    public int sub(int x, int y)
    {
        return (x - y);
    }
}


When we create the object of Pqr class then first it will call Pqr class constructor but Pqr class constructor first initialize the base class constructor then Pqr constructor will be initialized. It is very important point to note down the base class constructor initialized first.

Which of this keyword can be used in a subclass to call the constructor of parent class *?

Subclasses inherit public methods from the superclass that they extend, but they cannot access the private instance variables of the superclass directly and must use the public accessor and mutator methods. And subclasses do not inherit constructors from the superclass.

So, how do you initialize the superclass’ private variables if you don’t have direct access to them in the subclass? In Java, the superclass constructor can be called from the first line of a subclass constructor by using the special keyword super() and passing appropriate parameters, for example super(); or super(theName); as in the code below. The actual parameters given to super() are used to initialize the inherited instance variables, for example the name instance variable in the Person superclass.

public class Employee extends Person
{
    public Employee()
    {
        super(); // calls the Person() constructor
    }
    public Employee(String theName)
    {
        super(theName); // calls Person(theName) constructor
    }
}

Which of this keyword can be used in a subclass to call the constructor of parent class *?
Coding Exercise

The super(theName) in the Employee constructor will call the constructor that takes a String object in the Person class to set the name.

Try creating another Employee object in the main method that passes in your name and then use the get methods to print it out. Which class constructor sets the name? Which class constructor sets the id?

If a class has no constructor in Java, the compiler will add a no-argument constructor. A no-argument constructor is one that doesn’t have any parameters, for example public Person().

If a subclass has no call to a superclass constructor using super as the first line in a subclass constructor then the compiler will automatically add a super() call as the first line in a constructor. So, be sure to provide no-argument constructors in parent classes or be sure to use an explicit call to super() as the first line in the constructors of subclasses.

Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called since every class inherits from the Object class.

Which of this keyword can be used in a subclass to call the constructor of parent class *?
Check your understanding

    9-2-2: Given the class definitions of MPoint and NamedPoint below, which of the constructors that follow (labeled I, II, and III) would be valid in the NamedPoint class?

    class MPoint
    {
       private int myX; // coordinates
       private int myY;
    
       public MPoint( )
       {
          myX = 0;
          myY = 0;
       }
    
       public MPoint(int a, int b)
       {
          myX = a;
          myY = b;
       }
    
       // ... other methods not shown
    
    }
    
    public class NamedPoint extends MPoint
    {
       private String myName;
       // constructors go here
       // ... other methods not shown
    }
    
    //  Proposed constructors for this class:
    I.   public NamedPoint()
         {
            myName = "";
         }
    II.  public NamedPoint(int d1, int d2, String name)
         {
            myX = d1;
            myY = d2;
            myName = name;
         }
    III. public NamedPoint(int d1, int d2, String name)
         {
            super(d1, d2);
            myName = name;
         }
    

  • I only
  • I is okay but III is also okay.
  • I and III
  • The MPoint variables are private and they can not be directly accessed in NamedPoint. You can use super as the first line in a constructor to initialize them. If you don't use super as the first line in a constructor one will be put there by the compiler that will call the parent's no argument constructor.
  • II only
  • II is invalid. Children do not have direct access to private fields. You can use super in a constructor to initialize these by calling the parent's constructor with the same parameter list.
  • III only
  • I is also okay

You can step through this code using the Java Visualizer by clicking the following link Named Point.

9.2.1. Programming Challenge : Square is-a Rectangle¶

In this challenge, you are giving a class called Rectangle that has two instance variables, length and width, and two constructors that initialize them, and a method called draw() that uses nested loops to draw a length x width rectangle of stars. Try it out below.

You will write a new class called Square that inherits from Rectangle. Is a square a rectangle? Yes! A square is a rectangle where the length and width are equal. Square will inherit length, width, and the draw method. You will write square constructors that will call the Rectangle constructors.

  1. Make the class Square below inherit from Rectangle

  2. Add a Square no-argument constructor that calls Rectangle’s constructor using super().

  3. Add a Square constructor with 1 argument for a side that calls Rectangle’s constructor with 2 arguments using super.

  4. Uncomment the objects in the main method to test drawing the squares.

  5. Add an area() method to Rectangle that computes the area of the rectangle. Does it work for squares too? Test it.

  6. Add another subclass called LongRectangle which inherits from Rectangle but has the additional condition that the length is always 2 x the width. Write constructors for it and test it out.

Create a Square class that inherits from Rectangle.

For a more complex example of drawing shapes, try running this repl.it Java Swing code (or download the files here by clicking on Download on the top right and use the files in your own Java IDE). When the yellow panel comes up, click on either the Rectangle or the Oval button and then click and drag somewhere on the yellow panel to draw that shape. Take a look at the Rectangle.java and Oval.java files to see how they inherit from the Shape class in Shape.java. Java Swing graphical programming is not covered on the AP CS A exam, but it is a lot of fun!

9.2.2. Summary¶

  • Subclasses do not have access to the private instance variables in a superclass that they extend.

  • Constructors are not inherited.

  • The superclass constructor can be called from the first line of a subclass constructor by using the keyword super and passing appropriate parameters to set the private instance variables of the superclass.

  • The actual parameters passed in the call to the superclass constructor provide values that the constructor can use to initialize the object’s instance variables.

  • When a subclass’s constructor does not explicitly call a superclass’s constructor using super, Java inserts a call to the superclass’s no-argument constructor.

  • Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called. At this point, all of the constructors within the hierarchy execute beginning with the Object constructor.

You have attempted of activities on this page

Which keyword would be used in a subclass to call the constructor in a parent class?

Subclass Constructors Invocation of a superclass constructor must be the first line in the subclass constructor. super();

Which of this keyword can be 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 sub class to call the constructor of super class A Super B This C extent D extends?

The super keyword It is used to invoke the superclass constructor from subclass.

Which keyboard can be used to call the constructor of a parent class?

The super keyword can be used to invoke the parent class methods and constructors. It can also be used to access the fields of the parent class. The this keyword is used to represent the current instance of a class. It is used to access the instance variables and invoke current class methods and constructors.