Super Keyword In Java With Examples

In java the keyword super refers to the superclass of the class in which super appears. A subclass inherits the accessible variables and methods from its superclass, but the constructors of the superclass are not inherited in the subclass. They can only be invoked from constructors of subclass using the keyword super.

The superclass constructor call must be the first statement in the body of childclass constructor. If one does not specify super, the compiler implicitly inserts super(); at the beginning of the childclass’s default constructor to call the superclass’s default constructor.


Syntax of Super

Following syntax is used to call superclass constructor:

super([arguments])

Here the arguments enclosed in square bracket are optional.


Program Examples of Super Keyword

Program 1: This program shows how to call super class constructor using “super” keyword in Java.

Step 1: First we create a class SuperClass in which we take a constructor:

class SuperClass

{

  public SuperClass(String str)

  { 

    System.out.println("Super Class Constructor " + str);

  }

}

Step 2: Second we create a class SubClass which extends the class SuperClass and we use super keyword to call the SuperClass constructor:

class SubClass extends SuperClass

{

   public SubClass(String str)

   {

     super(str); //calls SuperClass class constructor

     System.out.println("Sub Class Constructor " + str);

   }

}

Step 3: Third we create a class SuperConst in which we make an object of class SubClass:

class SuperConst

{

   public static void main(String args[])

   {

     SubClass obj = new SubClass("called");

   }

}

Output:

Super Class Constructor called
Sub Class Constructor called

Program 2: This programs shows how to call super class method using “super” in java.

Step 1: First we create a class SuperClass in which we take a show() method:

class SuperClass

{

   void show()

  {

    System.out.println("Super Class method has been called");

  }

}

Step 2: Second we create a class SubClass which extends the class SuperClass and use the super keyword to call the SuperClass class method:

class SubClass extends SuperClass

{

  void show()

  {

    super.show();

    System.out.println("Sub Class method has been called");

  }

}

Step 3: Third we create a class SuperMethod in which we make an object of class SubClass:

class SuperMethod

{

  public static void main(String args[])

 {

   SubClass obj = new SubClass();

   obj.show();
 }

}

Output:

Super Class method has been called
Sub Class method has been called

Program 3: This program shows how to access super class variables using “super” in java.

Step 1: First we create a class SuperClass in which we take a variable:

class SuperClass

{

  int x = 30;

}

Step 2: Second we create a class SubClass which extends the class SuperClass and use the super keyword to call the SuperClass class variable:

class SubClass extends SuperClass

{

  void show()

 {

   int x = super.x;

   System.out.println("The value of x is : " + x);

 }

}

Step 3: Third we create a class SuperVar in which we make an object of class SubClass:

class SuperVar

{

   public static void main(String args[])

  {

    SubClass obj = new SubClass();

    obj.show();

  }

}

Output:

The value x is : 30

Rules of Super Keyword

  • The superclass constructor call, using keyword super must be the first statement in the body of subclass constructor.
  • A superclass constructor can only be called from a subclass constructor. Any other subclass method cannot call it.
  • A superclass constructor call requires the use of super. It is illegal to specify the actual name of the class.
  • From outside the class, a constructor is always called with a new From inside the class, it may be called from another constructor with this or super. “this” operator is used to call another constructor of the same class while super is used to call the constructor of its superclass.

Important Points to Remember:

  • We cannot print super, there is a syntax error.
  • Always data members of parent class are inherited by super keyword.
  • If we hide any variable then super keyword becomes mandatory.