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

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.

I think I'm missing something, or something isn't clicking for me. I'm reading a book on java, and I just learned about super(). When used to get a constructor, it gets the constructor from one superclass up, correct? What if you wanted to go two superclasses up, how would that be done?

Something's just not sitting right in my brain, and I'm hoping this question will help me put the pieces together.

The super keyword in Java is a reference variable which is used to refer immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

Usage of Java super Keyword

  1. super can be used to refer immediate parent class instance variable.
  2. super can be used to invoke immediate parent class method.
  3. super() can be used to invoke immediate parent class constructor.
Which keyword can be used in a subclass to call the constructor of superclass?

1) super is used to refer immediate parent class instance variable.

We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.

Test it Now

Output:

In the above example, Animal and Dog both classes have a common property color. If we print color property, it will print the color of current class by default. To access the parent property, we need to use super keyword.

2) super can be used to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.

Test it Now

Output:

In the above example Animal and Dog both classes have eat() method if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local.

To call the parent class method, we need to use super keyword.

3) super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:

Test it Now

Output:

animal is created
dog is created

Note: super() is added in each class constructor automatically by compiler if there is no super() or this().

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

As we know well that default constructor is provided by compiler automatically if there is no constructor. But, it also adds super() as the first statement.

Another example of super keyword where super() is provided by the compiler implicitly.

Test it Now

Output:

animal is created
dog is created

super example: real use

Let's see the real use of super keyword. Here, Emp class inherits Person class so all the properties of Person will be inherited to Emp by default. To initialize all the property, we are using parent class constructor from child class. In such way, we are reusing the parent class constructor.

Sometimes you want the subclass to do more than what a superclass’ method is doing. You want to still execute the superclass method, but you also want to override the method to do something else. But, since you have overridden the parent method how can you still call it? You can use super.method() to force the parent’s method to be called.

We’ve used super() before to call the superclass’ constructor. There are two uses of the keyword super:

  1. super(); or super(arguments); calls just the super constructor if put in as the first line of a subclass constructor.

  2. super.method(); calls a superclass’ method (not constructors).

The keyword super is very useful in allowing us to first execute the superclass method and then add on to it in the subclass.

Coding Exercise

In the example below, the Student class overrides the getFood() method of the Person() class, and it uses super.getFood() to call the Person getFood() method before adding on to it. Here, a Person is associated with the food “Hamburger” and a Student is associated with “Hamburger” and “Taco”.

Add another subclass called Vegan that inherits from the Student class. Add a Vegan contructor that takes a name as an argument and passes it to the super constructor. Override the getFood() method in Vegan to call the superclass getFood() but add a “No ” in front of it and then say “but ” and add a vegan food. Change Javier to a Vegan object in main() and try it out!

How does this work? Remember that an object always keeps a reference to the class that created it and always looks for a method during execution starting in the class that created it. If it finds the method in the class that created it, it will execute that method. If it doesn’t find it in the class that created it, it will look at the parent of that class. It will keep looking up the ancestor chain until it finds the method, all the way up to the Object class. The method has to be there, or else the code would not have compiled.

When the student getFood() method is executed it will start executing the getFood method in Student. When it gets to super.getFood() it will execute the getFood method in Person. This method will return the string "Hamburger". Then execution will continue in the getFood method of Student and return the string

// overridden toString() in subclass
public String toString()
{
  return super.toString() + "\n" + subclassInstanceVariables;
}
2.

Check your understanding

    9-4-2: Given the following class declarations, and assuming that the following declaration appears in a client program:

    // overridden toString() in subclass
    public String toString()
    {
      return super.toString() + "\n" + subclassInstanceVariables;
    }
    
    3, what is the result of the call
    // overridden toString() in subclass
    public String toString()
    {
      return super.toString() + "\n" + subclassInstanceVariables;
    }
    
    4?

    public class Base
    {
       public void methodOne()
       {
         System.out.print("A");
         methodTwo();
       }
    
       public void methodTwo()
       {
         System.out.print("B");
       }
    }
    
    public class Derived extends Base
    {
       public void methodOne()
       {
          super.methodOne();
          System.out.print("C");
       }
    
       public void methodTwo()
       {
         super.methodTwo();
         System.out.print("D");
       }
    }
    

  • AB
  • This would be true if the object was created of type Base using new Base. But the object is really a Derived object. So all methods are looked for starting with the Derived class.
  • ABDC
  • Even though b is declared as type Base it is created as an object of the Derived class, so all methods to it will be resolved starting with the Derived class. So the methodOne() in Derived will be called. This method first calls super.methodOne so this will invoke the method in the superclass (which is Base). So next the methodOne in Base will execute. This prints the letter "A" and invokes this.methodTwo(). Since b is really a Derived object, we check there first to see if it has a methodTwo. It does, so execution continues in Derived's methodTwo. This method invokes super.methodTwo. So this will invoke the method in the super class (Base) named methodTwo. This method prints the letter "B" and then returns. Next the execution returns from the call to the super.methodTwo and prints the letter "D". We return to the Base class methodOne and return from that to the Derived class methodOne and print the letter "C".
  • ABCD
  • After the call to methodOne in the super class printing "A", the code continues with the implicit this.methodTwo which resolves from the current object's class which is Derived. methodTwo in the Derived class is executed which then calls super.methodTwo which invokes printin "B" from methodTwo in the Base class. Then the "D" in the Derive methodTwo is printed. Finally the program returns to methodOne in the Derived class are prints "C".
  • ABC
  • The call to methodTwo in super.methodOne is to this.methodTwo which is the method from the Derived class. Consequently the "D" is also printed.
  • Nothing is printed.
  • Remember that it will first look for a method in its own class.

You can step through this example using the Java Visualizer by clicking on the following link: .

The toString() method is a common method that is overridden. A subclass can override the superclass toString() method and call the super.toString() before adding on its own instance variables.

// overridden toString() in subclass
public String toString()
{
  return super.toString() + "\n" + subclassInstanceVariables;
}

9.4.1. Programming Challenge : Customer Info

The Customer class below keeps track of the names and addresses of customers. It has a toString() method that prints out the name and address of the object.

  1. Create a subclass OnlineCustomer that inherits from the Customer class and adds a new instance variable for the email address of a online customer.

  2. Write an OnlineCustomer constructor that take 3 arguments, name, address, email, and passes the name and address to the super (Customer) constructor.

  3. Override the toString() method in the OnlineCustomer class to call the super class toString() method and then add on the email address. See the example above for help.

  4. Test the class by uncommenting the OnlineCustomer objects in the main method.

Complete the OnlineCustomer class below to inherit from Customer and add an email address, a constructor, and override the toString() method.

9.4.2. Summary

  • The keyword super can be used to call a superclass’s constructors and methods.

  • The superclass method can be called in a subclass by using the keyword super with the method name and passing appropriate parameters.

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

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

    Can be used in subclass to call the constructor of superclass?

    A subclass can call a constructor defined by its superclass by use of the following form of super: super(parameter-list); Here, parameter-list specifies any parameters needed by the constructor in the superclass.

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

    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.

    Which of this keyword can be used in a subclass to call the constructor of superclass select one a super B extent C extends D this?

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