Java Tutorial: How to use ‘super’ to Call Superclass Constructors

The “super” is a keyword defined in the Java programming language which means it is a reserved word to the compiler in Java.

Java “super” keyword

‘super’ is used mainly for two purposes:

  1. The ‘super’ keyword as a standalone statement is used to call theA�constructorA�of theA�superclassA�in the sub class.
  2. The ‘super’ keyword is used to resolve theA�overridden methodsA�and overcome in name hiding.

Here we will discuss the first usage of ‘super’.

The main fundamental is a subclass or class can initialize the instance variables of its superclass or parent class by the use of

super(parameter_list);A�where parameter_list = parameters needed by superclass constructor to initialize its class variables.

super(parameter_list) should always be the first statement inside the subclass constructor.

Now we will see how super works

Suppose a superclass has two variables declared inside it. Any object of this class will initialize these two variables using parameterized constructor of that particular class. Now a subclass which inherits the above mentioned superclass has one extra class variable. So, the subclass will have three class variables in total. Now any object of subclass will initialize all the three class variables through its own parameterized constructor. It can be accomplished in two ways. First option is the first program mentioned and the second option is the last program.

But if we follow the first option to accomplish our need, then there is a possibility of writing duplicate codes which is pretty much inefficient in terms of programming language. Meanwhile the same duplicate code can be replaced by super().

Again sometimes subclass may not have access to the superclass variables as there may be times when you may want to create a superclass that keeps the details of its implementation to itself. In this case, there would be no option available for a subclass to directly access or initialize these variables on its own. So there may be a curiosity since encapsulation is a primary attribute of OOP then how a subclass can initialize its superclass’s instance variables.The answer is sinple using super().

super() invokes the superclass constructor and initialize all the superclass instance variables with the corresponding values passed from subclass object.

Java Program

class SuperClass{
int number1;
int number2;

SuperClass(){
number1 = 1;
number2 = 1;
}

SuperClass(int number1,int number2){
this.number1 = number1;
this.number2 = number2;
}

void sum(){
System.out.println("Sum is: "+(number1+number2));
}

}

class SubClass extends SuperClass {
int number3;

SubClass(){
number1 = 1;
number2 = 1;
number3 = 1;
}

SubClass(int number1,int number2,int number3){
this.number1 = number1;
this.number2 = number2;
this.number3 = number3;
}

void show(){
System.out.println("Number3 is: "+number3);
}
}

class Main{
public static void main(String args[]){
SuperClass superClass = new SuperClass(1,2);
SubClass subClass = new SubClass(10,20,30);
System.out.println();
superClass.sum();

superClass.show(); // SuperClass object can't access to the members of it's subclass

System.out.println();

subClass.sum();

subClass.show();

}

}

is rewritten with the use of ‘super’

class SuperClass{
int number1;
int number2;
SuperClass(){
number1 = 1;
number2 = 1;
}

SuperClass(int number1,int number2){
this.number1 = number1;
this.number2 = number2;
}

void sum(){
System.out.println("Sum is: "+(number1+number2));
}
}

class SubClass extends SuperClass {
int number3;
SubClass(){
super();
number3 = 1;
}

SubClass(int number1,int number2,int number3){
super(number1,number2);
this.number3 = number3;
}

void show(){
System.out.println("Number3 is: "+number3);
}
}

class SuperDemo{
public static void main(String args[]){
SuperClass superClass = new SuperClass(1,2);
SubClass subClass = new SubClass(10,20,30);
System.out.println("Super Class...");
superClass.sum();
System.out.println();
System.out.println("Sub Class...");
subClass.sum();
subClass.show();
}
}

Output

super_constructor_invoke1

Explanation of Code and Output

In both the programs, in the SuperClass all the constructors are overloaded using default constructor and parameterized constructor. As SubClass has inherited the SuperClass it will also have overloaded constructors similar to SuperClass in order to initialize all the instance variables of the SuperClass in addition to initialization ofA� SubClass’s own instance variable.

If you observe the first program then you will find that within the constructors of SubClass, the same code is written time and again as written in the constructors of SuperClass.

But in the second program, we have used super() and super(number1,number2) to initialize the instance variables of SuperClass. That does mean using super() statement we have not written any duplicate code time and again.

Flash On

  • Remember super() must be the first statement in the subclass’s or class’s constructor which implies first you have to initialize the instance variables of its superclass followed by the initialization of its own instance variables.
  • If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the default constructor of the superclass. If the super class does not have a default constructor, you will get a compile-time error.
  • If a subclass constructor either explicitly or implicitly invokes a constructor of its superclass, there will be a whole series of constructors called, all the way back to the constructor of Object, so calledA�constructor chaining, and you need to be aware of it when there is a long line of class descent.

Leave a Reply

Your email address will not be published. Required fields are marked *

Show my latest post here