Use of super keyword in java.

The super keyword refers to the objects of immediate parent class. 

The use of super keyword.
1) To access the data members of parent class when both parent and child class have member with same name.
2) To explicitly call the no-arg and parameterized constructor of parent class.
3) To access the method of parent class when child class has overridden that method.

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

When you have a variable in child class which is already present in the parent class then in order to access the variable of parent class, you need to use the super keyword.

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);//output will be 100.
   }
   public static void main(String args[]){
	Subclass obj= new Subclass();
	obj.printNumber();	
   }
}

By calling a variable like this, we can access the variable of parent class if both the classes (parent and child) have same variable.

super.variable_name

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

We can call super() explicitly in the constructor of child class, but it would not make any sense because it would be redundant. It’s like explicitly doing something which would be implicitly done otherwise.
However when we have a constructor in parent class that takes arguments then we can use parameterized super, like super(100); to invoke parameterized constructor of parent class from the constructor of child class.

class Parentclass
{
   
   Parentclass(){
	System.out.println("no-arg constructor of parent class");
   }
   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("java");
	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 will be;- parameterized constructor of parent class
                                                           Constructor of child class
                                                            Hello
   }
}

There are few important points to note in this example:
1) super()(or parameterized super must be the first statement in constructor otherwise you will get the compilation error: “Constructor call must be the first statement in a constructor”
2) When we explicitly placed super in the constructor, the java compiler didn’t call the default no-arg constructor of parent class.

Posted on by