Super Java Definition

Super keyword in Java is a reference variable. It is used to call an immediate parent’s class constructor.

Moreover, when we create an object of subclass/child an instance of parent class is created implicitly.

Finally, this instance is referred to by a super reference variable. If the parent class instance is not created then the parent class constructor can not be called. Thus, parent class will not load.

What does super do in Java?

There are 3 uses of super keyword in Java
  1. To refer to an immediate parent class instance variable.
  2. To invoke the immediate parent class method.
  3. Java super() can be use to invoke an immediate parent class constructor.

Super to refer to immediate parent class instance variable

If a parent and a child class have the same fields then we use the super keyword to access the parent class field.

Without a super keyword in Java, it is not possible to access the parent class field because priority is given to local.

Java Example 1


public class Animal { //animal class is the parent class
	String colour="Brown";
}
public class Dog extends Animal{//dog inherit animal
	String colour="White";
	void display()
	{
		System.out.println("Colour of the animal is "+super.colour);
                //if we don't use super, present class colour property will be called
		System.out.println("Colour of the dog is "+colour);
	}
}
public class Super1 { // class with main method

	public static void main(String[] args) {
		Dog obj=new Dog();
		obj.display();

	}

}

Output

java super example output

java super example output

To invoke the immediate parent class method

Likewise, when there is method overriding we use the super keyword to call the parent class method.

Java Example 2


public class Animal {
	void eat()
	{
		System.out.println("Animal eats grass");
	}
}
public class Dog extends Animal{
	void eat()
	{
		System.out.println("Dog eats meat");
	}
	void display()
	{
		eat();
		super.eat();
	}
}
public class Super2 {

	public static void main(String[] args) {
		Dog obj=new Dog();
		obj.display();

	}

}

Note: In the above code if we did not use super.eat() then eat method of subclass would have invoked again.

Output

super java output example

Output

super() is used to invoke parent class constructor

Java Example 3


public class Animal {
	Animal()
	{
		System.out.println("I am in animal");
	}
}
public class Dog extends Animal{
	Dog()
	{
		System.out.println("I am in Dog");
	}
}
public class Super3 {

	public static void main(String[] args) {
		Dog obj=new Dog();

	}

}

Output

super constructor call of parent

output

You might be in a doubt after checking the output that we have created an instance of child class but the parent class constructor is being invoked first?

Answer: Compiler implicitly adds super() keyword in subclass constructor. super() refers parent class constructor.
So, when we create an instance/object of subclass both classes constructor gets invoked.

Example to demonstrate real use of super() keyword.
Reusing the parent class constructor.


public class Employee {
	int emp_id;
	String emp_name;
	Employee(int emp_id,String emp_name)
	{
		this.emp_id=emp_id;
		this.emp_name=emp_name;
		
	}

}
public class Manager extends Employee {
	int manager_id;
	Manager(int emp_id, String emp_name,int manager_id) {
		super(emp_id, emp_name);//using parent class constructor
		this.manager_id=manager_id;
		
	}
	void display()
	{
		System.out.println("emp_id:"+emp_id+" ,name: " + emp_name+" ,manager_id: "+manager_id);
	}
	
	

}
public class Super4 {

	public static void main(String[] args) {
		Manager obj=new Manager(101, "Arka", 12234);
		obj.display();

	}

}

Output

use of super() keyword Java

Output

The above example demonstrates- How we can reuse the parent class constructor to set values?

When we create an instance of subclass(Manager) we invoke a subclass constructor which in turn invoke the parent class(Employee) constructor.

✌️ Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed