The super() keyword is used to reference objects for the immediate parent class. A subclass inherits the accessible data fields 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 the subclass( es) using the keyword super.
The keyword super refers to superclass of the class in which super appears. The syntax used to call superclass constructor is
super([arguments])
Here, the arguments enclosed in square brackets are optional. The statement super () invokes the constructor with no parameters of its superclass and the statement
super (arguments)
invokes the superclass constructor that matches the arguments. The superclass constructor call must be the first statement in the body of subclass constructor. If one does not specify super, the compiler implicitly inserts super (); at the beginning of the subclass’s default constructor to call the superclass’s default constructor.
When you create an instance of the subclass, it invokes the constructors of all the superclasses along the inheritance chain. A superclass constructor is always called before the superclass constructor.
Now let us consider a program to explain the use of super keyword.
class Point //superclass { private int x,y,z; Point(int xl,int yl,int zl) { System.out.println("Super class constructor is invoked"); x = xl; y = yl; z = zl; } public int getX() {return x;} public int getY() {return y;} public int getZ() {return z;} } class Sphere extends Point //Subclass { private int radius; Sphere(int xl,int yl,int zl,int r) { super(xl,yl,zl); //call point(int xl,int yl,int zl); System.out.println("Derived class constructor is invoked"); radius = r ; } public int getRadius() {return radius; } public double volume() { return (4*Math.PI*Math.pow(radius,3)); } } public class SuperKeyword { public static void main(String[] args) { Sphere s = new Sphere(2,3,4,5); //create Sphere object System.out.println("x coordinate = " +s.getX()); System.out.println("y coordinate = " +s.getY()); System.out.println("z coordinate = " +s.getZ()); System.out.println("Volume of Sphere is = " +s.volume()); } }
We’ll be covering the following topics in this tutorial:
The super() keyword is used:
• For accessing the parent class data members when the parent and the child contain a member with identical names.
• For explicitly calling the parametrized and no-arg constructors for the parent class.
• For accessing the parent class method when it has been overridden by the child class.
Now let’s see some examples to explain these:
Accessing Parent Class Variables
When you have a child class that contains a variable that is also in the parent class, when you want access to the parent class variable the super() keyword is needed. In the next code example, we have declared a data member called num in our parent class. We cannot access the variable for num in our parent class without the super() keyword:
//Parent class, Superclass or base class
class Superclass {
int num =150;
}
//Child class, subclass or derived class
class Subclass extends Superclass {
/* The variable called num which is in the superclass
* is also present in the subclass */
int num =160;
void printNumber(){
System.out.println(num);
}
public static void main(String args[]){
Subclass obj=new Subclass();
obj.printNumber();
}
}
The output of this will be: 160
To access the num variable in the parent class, we can call it in the following way, provided the parent, and the child class contains the same variable:
super.variable_name
Another example: this we have a print statement where, rather than num, we pass super. num instead:
class Superclass {
int num =150;
}
class Subclass extends Superclass {
int num =160;
void printNumber(){
/* Rather than using num, we are using
* super.num in our print statement
* referencing the num variable of Superclass */
System.out.println(super.num);
}
public static void main(String args[]) {
Subclass obj=new Subclass();
obj.printNumber();
}
}
Output: 150
By using super.num instead of num, we can gain access to the parent class num variable.
Invoking the Parent Class Constructor
The object for the subclass is created, we use the new keyword for invoking the child class constructor. In turn, this implicitly invokes the parent class constructor. The execution order when the child class object is created is a constructor for the parent class gets executed first, followed by the constructor for the child class. The reason for this order is that the compile inserts super() to invoke the parent class no-arg constructor as the initial statement in the child class constructor.
Let’s see this by way of an example:
class Parentclass {
Parentclass(){
System.out.println(“Constructor of parent class”);
}
}
class Subclass extends Parentclass {
Subclass(){
/* Compile will implicitly add super() at this point as the
* initial statement for this constructor. */
System.out.println(“Constructor of child class”);
}
Subclass(int num) {
/* Although it is a parameterized constructor.
* The compiler will still insert the no-arg super() at this point */
System.out.println(“arg constructor of child class”);
}
void display() {
System.out.println(“Hello!”);
}
public static void main(String args[]) {
/* The default constructor is used for creating the object and this
* invokes the child class constructor, which, in turn
* invokes the constructor for the parent class */
Subclass obj = new Subclass();
//We are now calling the subclass method
obj.display();
/* We use the arg constructor to create another object
* which invokes the child class constructor which, in turn
* will invoke the parent class no-arg constructor automatically */
Subclass obj2 = new Subclass(10);
obj2.display();
}
}
The output for this will be:
Constructor of parent class
Constructor of child class
Hello!
Constructor of parent class
arg constructor of child class
Hello!
Using a Parametrized Super() Call
For the child class constructor, we can explicitly call super(), but it makes no sense to do so because it would only be redundant. However, for a parent class constructor that takes parameters, we would use a parametrized super() call instead, as a way of invoking the parent class’s parametrized constructor from the child class’s constructor.
An example will make this clearer:
class Parentclass {
//no-arg constructor
Parentclass() {
System.out.println(“no-arg constructor of parent class”);
}
//arg or parameterized constructor
Parentclass(String str) {
System.out.println(“parameterized constructor of parent class”);
}
}
class Subclass extends Parentclass {
Subclass() {
/* super() must be added to the initial statement of the constructor
* otherwise you get a compilation error. Another important
* point is that when super is explicitly used in the constructor,
* the compiler will not invoke the parent constructor automatically. */
super(“Hahaha”);
System.out.println(“Constructor of child class”);
}
void display(){
System.out.println(“Hello”);
}
public static void main(String args[]) {
Subclass obj=new Subclass();
obj.display();
}
}
The output of this will be:
parameterized constructor of parent class
Constructor of child class
Hello
This particular example throws up two critical points:
You must have either super() or a parametrized super() as the initial constructor statement otherwise the compilation error we talked of earlier will be thrown. When super() was placed explicitly in the constructor, note that the no-arg constructor for the parent class was not called by the compiler.
The Super() Keyword and Method Overriding
When a method that is already in the parent class is declared by the child class, it is called overriding. We will talk more about this later; for now, keep in mind that when child classes override parent class methods when a method is called form the child class object, it will be the child version of the method. However, when you use super() in this way – super.method_name, the parent class method, the one that has been overridden, may be called. Have a look at an example:
class Parentclass {
//The method that has been overridden
void display(){
System.out.println(“Parent class method”);
}
}
class Subclass extends Parentclass {
//The method doing the overriding
void display(){
System.out.println(“Child class method”);
}
void printMsg() {
//This calls the Overriding method
display();
//This calls the Overridden method
super.display();
}
public static void main(String args[]) {
Subclass obj=new Subclass();
obj.printMsg();
}
}
The output of this would be:
Child class method
Parent class method