Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Super Keyword?
3.
Usage of Java Super Keyword
4.
Use of super keyword with variable
4.1.
Example
5.
Use of super keyword with methods
5.1.
Example
6.
Use of super with constructors
6.1.
Example
7.
Characteristics of Super Keyword
8.
Advantages of Super Keyword In Java
9.
Disadvantages of Super Keyword In Java
10.
Frequently Asked Questions
10.1.
What is the difference between this () and super () in Java? 
10.2.
What are the three usages of Java super keyword?
10.3.
Why is Super important in Java?
11.
Conclusion
Last Updated: Mar 28, 2024
Easy

Super Keyword in Java

Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

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.

Super Keyword in Java

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.

Also see, Duck Number in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Usage of Java Super Keyword

Usage of Java Super Keyword
  • Use of super keyword with variable
     
  • Use of super keyword with methods
     
  • Use of super with constructors
     

Let's discuss them one by one.

Also see,  Swap Function in Java

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. 

Example

/* 
	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

Value of Num in child class: 100
Value of Num in parent class: 120


Explanation

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 of ParentClass is called.

You can practice by yourself with the help of the Online Java Compiler.

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 as 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.

Example

/* 
	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


Explanation

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 of ParentClass is called.

Learn more about, Hashcode Method in Java here.

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 parametrized as well as non-parameterized constructors.

For example, consider the code snippet.

Example

/* 
	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


Explanation

  • 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-parameterized 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.

 

Must Read Type Conversion in Java

Characteristics of Super Keyword

The following are the characteristics of the Super Keyword:

  • In Java, the term super keyword refers to the parent class of a subclass.
     
  • It can be used to access or invoke parent-class objects from within the subclass.
     
  • This contains constructors, methods, and variables from the parent class.
     
  • If the subclass does not specify its own constructor, the parent class function will be called automatically using super().
     
  • Super can also be used to reach parent class instance variables or methods that the subclass has hidden.
     
  • When used with methods, super is used to invoke the parent class method rather than the subclass's overridden method.

Advantages of Super Keyword In Java

The following are the advantages of the Super Keyword:

  • Allows access to parent class members such as instance variables, instance methods, and constructors.
     
  • Enables reuse of code that already exists in the parent class.
     
  • Facilitates method overriding by allowing a subclass to call the parent class's version of an overridden method using super.
     
  • Provides a way to explicitly call a specific constructor of a superclass when creating an instance of the subclass.
     
  • Can be used to resolve name conflicts between subclass members and parent classes.
     
  • Maintains inheritance relationships between classes, making your code cleaner and easier to understand.
     
  • It makes your code more flexible and extensible, making future changes and extensions easier. 
     

Disadvantages of Super Keyword In Java

The following are the disadvantages of the Super Keyword:

  • Overuse or improper use of Super Keyword will make your code difficult to understand.
  • It can sometimes be unclear which methods are being called, especially in large inheritance hierarchies.
  • The use of "super" can break encapsulation if it's used to access parent class's variables or methods that should be private.
  • If you use Super Keyword a lot, your code can become too dependent on the parent class. This can cause problems if you need to make changes to the parent class later on.

Must Read Conditional Statements in Java

Frequently Asked Questions

What is the difference between this () and super () in Java? 

This() is used in Java to call a constructor within the same class, whereas super() is used to call a constructor in the master class. This refers to the present instance of the class, whereas super denotes the parent class.

What are the three usages of Java super keyword?

In Java, the super keyword can be used to refer to the parent class of a subclass and has three main usages:

  1. Calling a parent class constructor.
  2. Accessing a parent class method or instance variable.
  3. Resolving name conflicts between a subclass and a parent class.

Why is Super important in Java?

The Super keyword in Java is important as it allows for Inheritance, Method Overriding, and Polymorphism. It helps the developers write more efficient and clean code by using the power of Object Oriented Programming.

Conclusion

This article discussed the super keyword in Java. Super keyword in Java helps create a bridge between child and parent class. The super keyword in Java can be used to access the parent class instance variable. It can also invoke the parent class method and call or invoke the parent class constructor.

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.

Previous article
Compile Time Polymorphism in Java
Next article
Encapsulation in Java
Live masterclass