What Is A Super Keyword In Java?

What Is Super Keyword In Java?
What Is Super Keyword In Java?

Introduction

Java is one of the most popular programming languages. It is used for many applications ranging from Blu-ray disk players to rovers in outer space. Java was developed in 1991, and even after 30 years, it is one of the most popular languages and is used in over five billion devices. Portability and platform independence are some of the reasons for the popularity of Java.

Java is an object-oriented programming language that uses objects, classes, and keywords. One such keyword is ‘super.’ Super keyword in Java helps create a bridge between child and parent class.

Today in this blog, we will discuss the super keyword in detail and will critically examine the super keyword in Java with some practical examples.

What is a Super Keyword?

Super keyword in Java is a reference variable used to refer to the immediate parent class(superclass) object. The super keyword in Java is used when we want to acquire a superclass member and use it in a derived class. When an instance of the child class is created, an instance of the superclass is also created.

The super keyword in Java comes into play with the concept of inheritance. So it is important to have a basic understanding of inheritance.

The super keyword in Java is used in the following scenarios:

  • super can be used to refer to a variable of the immediate parent class
  • super can be used to invoke the immediate parent class method.
  • super() can be used to access the immediate parent class constructor.

Let’s discuss each application of super keywords in detail with examples.

Use of super keyword with variable

The super keyword in Java can be used to access the parent class instance variable. The Super keyword is very useful when both child and parent classes have the same data members. If both child and parent classes have the same data member, then there is a possibility of ambiguity for Java Virtual Machine(JVM) which can be avoided using super keyword.

For example, consider the code snippet.

/* Parent class */
class ParentClass
{
	int num = 120;
	
}

/* sub class childClass extending parentClass */
class ChildClass extends ParentClass
{
	int num = 100;

	void display()
	{
		//printing the num without use of super keyword 
		System.out.println("Value of Num in child class: " + num);
		
		// printing the num with use of super keyword 
		System.out.println("Value of Num in parent class: " + super.num);
	}
}
public class Main{

     	public static void main(String[] args)
	{
		ChildClass a = new ChildClass();
		a.display();
	}
}

Output:

blog banner 1
Value of Num in child class: 100
Value of Num in parent class: 120

Notice how in the above code, num is a common data member in both classes. When num is called without the super keyword, the value of num in ChildClass is printed, but with super keyword, the num variable ofParentClass is called.

Use of super keyword with methods

Super keyword in Java can also be used to invoke the parent class method. The super keyword is used when both child and parent classes have the same method, and we want to call the parent class method. 

When a parent class and child class have the same method, the parent class method is overridden by the child class method called method overriding in Java. If we want to call the parent class method in a child class, we should use super keywords to achieve this.

For example, consider the code snippet.

/* Parent class */
class ParentClass
{
	void function()
	{
	    System.out.println("This is method of parent class");
	}
	
}

/* sub class childClass extending parentClass */
class ChildClass extends ParentClass
{
    void function()
	{
		 System.out.println("This is method of child class");
	
    }
    
	void display()
	{    //will inwoke parent class function()
		 super.function();
		 
		 //will invoke current(child class) function()
		 function();
	}
}

public class Main{

     	public static void main(String[] args)
	{
		ChildClass a=new ChildClass();
		a.display();
	}
}

Output:

This is method of parent class
This is method of child class

Notice how in the above code, when function() is called without the super keyword, the value of function() of ChildClass is printed but with super keyword, the function() method ofParentClass is called.

Use of super with constructors

The super keyword in Java can also be used to call or invoke the parent class constructor. Super can be used to call both parametrised as well as non-parameterized constructors.

For example, consider the code snippet.

/* Parent class */
class ParentClass
{
    //parent class constructor
	ParentClass()
	{
	    System.out.println("This is constructor parent class");
	}
	
}

/* sub class childClass extending parentClass */
class ChildClass extends ParentClass
{
    //child class constructor
    ChildClass()
	{
           //call parent constructor
	      super();
		 
           System.out.println("This is constructor of child class");
	
    }
    

}

public class Main{

     	public static void main(String[] args)
	{
		ChildClass a=new ChildClass();
		
	}
}

Output:

This is constructor parent class
This is constructor of child class

Note:

  • super() must be the first statement in the child class constructor.
  • If a child class constructor doesn’t contain super, then the compiler automatically inserts a non-parameterised super keyword at the start of the constructor. If the parent class has a parameterized constructor, and you haven’t used super() then the compiler will throw an error.

Frequently Asked Questions

What is called using super ()?

Using super(), we can call the parent class constructor, and super() must be the first statement in the child class constructor.

What is the difference between this and super keyword in Java?

The “this” keyword points to a reference of the current class whereas the super keyword in java points to the parent class reference. Super keyword in Java can be used to access variables and methods of the parent class, whereas this keyword is used to access variables and methods of the current class.

Is super () necessary?

super() is necessary if the parent constructor is a parameterised constructor, whereas if the parent class constructor is non-parameterized, then super() is not necessary.

What is super() in python?

The super() keyword in python is used to get an object that allows reference to the parent class. Using super(), we can access all the functions and variables in the parent class.

What does super() _ init_ do?

super()init is used to call the immediate parent class constructor in python.

Key Takeaways

This article discussed the super keyword in Java. It also discussed super keyword with ways to implement them with codes in Java. The article discussed how to use super keyword with variables, methods, and constructors.

If you want to learn Java you can look out for our Java guided path a complete curated preparation guide for coding interviews at tech and product-based companies.

By Pranchal Agrahari