A subclass inherits the data members and methods from its super or parent class but the constructors of super class are not inherited by the sub class. The sub class may have its own constructors. A sub class can invoke the constructors of the parent class using the Java super keyword.

The super keyword can be used in the following contexts:

Java Super Keyword

Java Super Keyword with Variables

This scenario occurs when both the parent and the child classes have the same data members. This is done so that there is no ambiguity for the JVM so as to which member variable is to be referred.

Example:

 
 
class Vehicle
{
      int speed = 110;
}
class Bike extends Vehicle
{
      int speed = 70;
      void show()
      {
            System.out.println("Max speed = " + super.speed);
      }
}
class superVar
{
      public static void main(String args[])
      {
            Bike b = new Bike();
            b.show();
      }
}

Output:

Java Super Keyword with Variables

In this example, both the classes have the same variable speed. Here, we are accessing the speed variable of the parent class in the sub class with the help of the super keyword.

Java Super Keyword with Methods

This is used when we want to make a call to the parent class method. So when the parent and the child class have the same name for a method, so to resolve the ambiguity we make use of the super keyword to access the parent class method in the base class.

Example:

 
 
class pClass
{
      void msg()
      {
            System.out.println("Parent Class");
      }
}
class sub extends pClass
{
      void msg()
      {
            System.out.println("Child class");
      }
      void show()
      {
            msg();            // invokes child class method
            super.msg();      // invokes parent class method
      }
}
class superMember
{
      public static void main(String args[])
      {
            sub s = new sub();
            s.show();
      }
}

Output:

Java Super Keyword with Method

In the above example we can clearly see that when we call the method by its name only then the method of the child class is invoked and when we make use of the super keyword, then the method of the super class is invoked.

Java Super Keyword with Constructors

Super keyword is also used to access the constructors of the parent class. As constructors are both parameterised and non-parameterised, we can make use of the super keyword to call either of these constructors depending on the situation.

Example

 
 
class pClass
{
      pClass()
      {
            System.out.println("Parent Class");
      }
}
class sub extends pClass
{
      sub()
      {
            super();            // Call to parent constructor
            System.out.println("Child class");
      }
}
class superConst
{
      public static void main(String args[])
      {
            sub s = new sub();
      }
}

Output:

Java Super Keyword with Constructor

In the above example, we have called the super / parent class constructor using the super() keyword via the sub / child class.

Important points to remember

  • Call to super() MUST be the first statement in the derived / child class constructor. Else a compile time error message will be displayed on the screen.
  • If the constructor of the derived class does not explicitly call or invoke the base class constructor then the Java compiler will take over. It will automatically insert a call to the no-argument or the default constructor of the super class. If the super class does not have a default constructor a compile time error will be displayed on the screen.
  • When the constructor of the derived class invokes a constructor of the parent class, either explicitly or implicitly, a whole chain of constructors is called all the way back to the constructor of object. This is known as constructor chaining.