Java

Super Keyword In Java

The immediate parent class objects are referred to using the super keyword. To comprehend the examples offered in this article, you must have a working grasp of Java’s inheritance system before learning how to use the super keyword.

The use of super keyword

  1. When a member of the parent class and the child class have the same name, it is possible to retrieve their data.
  2. To specifically invoke the parent class’s parameterized, no-arg constructor.
  3. To access a parent class method that a child class has overridden.

1) How to use super keyword to access the variables of parent class

When a child class has a variable that is also present in the parent class, you must use the super keyword to access the parent class variable.

To further grasp this, let’s look at an example: The data member num is defined in the child class of the following program even if the parent class already has a member with the same name. You cannot access the parent class’s num variable without using the super keyword.

//Parent class or Superclass or base class
class Superclass
{
   int num = 100;
}
//Child class or subclass or derived class
class Subclass extends Superclass
{
   /* The same variable num is declared in the Subclass
    * which is already present in the Superclass
    */
    int num = 110;
    void printNumber(){
	System.out.println(num);
    }
    public static void main(String args[]){
	Subclass obj= new Subclass();
	obj.printNumber();	
    }
}

Output:
110

Accessing the num variable of parent class:

If both classes (parent and child) share the same variable, we may access it by calling a variable in this way.

super.variable_name

Take the same example we saw earlier, but this time we are providing super.num to the print statement instead of num.

class Superclass
{
   int num = 100;
}
class Subclass extends Superclass
{
   int num = 110;
   void printNumber(){
	/* Note that instead of writing num we are
	 * writing super.num in the print statement
	 * this refers to the num variable of Superclass
	 */
	System.out.println(super.num);
   }
   public static void main(String args[]){
	Subclass obj= new Subclass();
	obj.printNumber();	
   }
}

Output:
100

2) Use of super keyword to invoke constructor of parent class

The new keyword calls the constructor of the child class, which indirectly calls the constructor of the parent class, when we create an object of the sub class. Therefore, the parent class constructor is called first, followed by the child class constructor, when we create an object of a child class. This occurs because super(), which calls the no-arg constructor of the parent class, is automatically added by the compiler as the first line in the constructor of the child class.

To better comprehend what I just said, let’s look at an example:

class Parentclass
{
   Parentclass(){
	System.out.println("Constructor of parent class");
   }
}
class Subclass extends Parentclass
{
   Subclass(){
	/* Compile implicitly adds super() here as the
	 *  first statement of this constructor.
	 */
	System.out.println("Constructor of child class");
   }
   Subclass(int num){
	/* Even though it is a parameterized constructor.
	 * The compiler still adds the no-arg super() here
	 */
	System.out.println("arg constructor of child class");
   }
   void display(){
	System.out.println("Hello!");
   }
   public static void main(String args[]){
	/* Creating object using default constructor. This 
	 * will invoke child class constructor, which will 
	 * invoke parent class constructor
	 */
	Subclass obj= new Subclass();
	//Calling sub class method 
	obj.display();
	/* Creating second object using arg constructor
	 * it will invoke arg constructor of child class which will
	 * invoke no-arg constructor of parent class automatically 
	 */
	Subclass obj2= new Subclass(10);
	obj2.display();
   }
}

Output:

Constructor of parent class
Constructor of child class
Hello!
Constructor of parent class
arg constructor of child class
Hello!

Parameterized super() call to invoke parameterized constructor of parent class

Although it is possible to explicitly call super() in a child class’ constructor, doing so would be unnecessary and useless. It’s similar to performing something explicitly that would otherwise be done implicitly.

However, if the parent class’ constructor accepts parameters, we may use parameterized super, such as super(100); to call the parent class’ constructor from the child class’ constructor.
To further grasp this, let’s look at an example:

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 first statement of constructor 
	* otherwise you will get a compilation error. Another important 
	* point to note is that when we explicitly use super in constructor
	* the compiler doesn't 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();	 
   }
}

Output:

parameterized constructor of parent class
Constructor of child class
Hello

3) How to use super keyword in case of method overriding

Method overriding is the act of a child class declaring a method that already exists in the parent class. The next lessons in this series will cover method overriding. For now, simply keep in mind this: When a parent class method is overridden by a child class, the child class version of the method is always invoked when the parent class method is called from an object of the child class. However, calling the method of the parent class with the super keyword looks like this: super.method name (the method which is overridden). These terms are used in method overriding situations: Overriding technique: The parent class’s method The child class’s method is the overriding method. Let’s use an illustration to help you grasp this idea:

class Parentclass
{
   //Overridden method
   void display(){
	System.out.println("Parent class method");
   }
}
class Subclass extends Parentclass
{
   //Overriding method
   void display(){
	System.out.println("Child class method");
   }
   void printMsg(){
	//This would call Overriding method
	display();
	//This would call Overridden method
	super.display();
   }
   public static void main(String args[]){		
	Subclass obj= new Subclass();
	obj.printMsg(); 
   }
}

Output:

Child class method
Parent class method

What if the child class is not overriding any method: No need of super

When the parent class method is not overridden by the child class, the super keyword is not required when calling the parent class method. This is so that we may directly use the methods of the parent class without using super, since there is only one instance of each method in this situation, and the child class has access to the parent class methods.

class Parentclass
{
   void display(){
	System.out.println("Parent class method");
   }
}
class Subclass extends Parentclass
{
   void printMsg(){
	/* This would call method of parent class,
	 * no need to use super keyword because no other
	 * method with the same name is present in this class
	 */
	display();
   } 
   public static void main(String args[]){
		
	Subclass obj= new Subclass();
        obj.printMsg(); 
   }
}

Output:

Parent class method

Related Articles

Leave a Reply

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

Back to top button