Table of contents
1.
Introduction
2.
What is a Java Super Keyword?
3.
Uses of Java Super Keyword
4.
Use of super keyword with variable
4.1.
Example
4.2.
Java
5.
Use of super keyword with methods
5.1.
Example
5.2.
Java
6.
Use of super with constructors
6.1.
Example
6.2.
Java
7.
Super Keyword is used to refer immediate parent class instance variable
7.1.
Java
8.
Super Keyword can be used to invoke parent class method
8.1.
Java
9.
Super Keyword is used to invoke the parent class constructor
9.1.
Java
10.
Characteristics of Super Keyword in Java
11.
Advantages of Super Keyword In Java
12.
Disadvantages of Super Keyword In Java
13.
Frequently Asked Questions
13.1.
What is super() in class?
13.2.
Why is super() used in Java?
13.3.
What is super and this keyword in Java?
14.
Conclusion
Last Updated: Nov 16, 2024
Easy

Super Keyword in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 Java 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

Uses of Java Super Keyword

Usage of Java Super Keyword
  1. Use of super keyword with variable
     
  2. Use of super keyword with methods
     
  3. 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

  • Java

Java

/* 
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();
}
}
You can also try this code with Online Java Compiler
Run Code


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

  • Java

Java

/* 
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();
}
}
You can also try this code with Online Java Compiler
Run Code


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

  • Java

Java

/* 
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();
}
}
You can also try this code with Online Java Compiler
Run Code


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

Super Keyword is used to refer immediate parent class instance variable

In Java, the `super` keyword is used to refer to the immediate parent class of the current class. When a subclass inherits from a superclass, it can access and use the members (variables and methods) of the superclass using the `super` keyword.

The main uses of `super` is to refer to the instance variables of the immediate parent class, especially when the subclass has an instance variable with the same name as the superclass. In such cases, using `super` helps to differentiate between the instance variables of the subclass and the superclass.

For example : 
 

  • Java

Java

class Vehicle {
protected String brand = "Generic";

public void displayBrand() {
System.out.println("Brand: " + brand);
}
}

class Car extends Vehicle {
private String brand = "Car";

public void displayBrand() {
super.displayBrand(); // Calls the superclass displayBrand() method
System.out.println("Brand in subclass: " + brand);
System.out.println("Brand in superclass: " + super.brand);
}
}

public class Main {
public static void main(String[] args) {
Car car = new Car();
car.displayBrand();
}
}
You can also try this code with Online Java Compiler
Run Code

In this example, we have a superclass `Vehicle` with an instance variable `brand` and a method `displayBrand()`. The subclass `Car` inherits from `Vehicle` and has its own instance variable `brand` with a different value.

Inside the `displayBrand()` method of the `Car` class, we use `super.displayBrand()` to call the `displayBrand()` method of the superclass. This allows us to display the value of the `brand` instance variable from the superclass.

We also use `super.brand` to refer to the `brand` instance variable of the superclass directly. This is useful when we want to access the superclass's instance variable, even if the subclass has an instance variable with the same name.

When we run the `Main` class and call `car.displayBrand()`, the output will be:
 


Brand: Generic
Brand in subclass: Car
Brand in superclass: Generic

As you can see, using `super` allows us to refer to the immediate parent class instance variable and access it explicitly, even when the subclass has an instance variable with the same name.

Note: It's important to remember that `super` can also be used to call the constructor of the immediate parent class and to invoke other methods of the superclass. It provides a way to reuse and extend the functionality of the parent class in the subclass while maintaining the proper inheritance hierarchy.

Super Keyword can be used to invoke parent class method

In Java, the `super` keyword can be used to invoke a method of the immediate parent class from within the subclass. This is very useful when the subclass overrides a method defined in the superclass but still wants to call the superclass's implementation of that method.

When a subclass overrides a method from its superclass, it provides its own implementation of that method. However, there may be cases where the subclass wants to extend the functionality of the superclass's method rather than completely replacing it. In such situations, the subclass can use the `super` keyword followed by the method name to invoke the parent class's method.

Let's look at an example to show the use of `super` to invoke a parent class method:

  • Java

Java

class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
public void makeSound() {
super.makeSound(); // Invokes the makeSound() method of the Animal class
System.out.println("Dog barks");
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound();
}
}
You can also try this code with Online Java Compiler
Run Code

In this example, we have a superclass `Animal` with a method `makeSound()` that prints a generic sound message. The subclass `Dog` extends the `Animal` class and overrides the `makeSound()` method.

Inside the `makeSound()` method of the `Dog` class, we use `super.makeSound()` to invoke the `makeSound()` method of the `Animal` class. This ensures that the generic sound message from the superclass is printed first.

After invoking the superclass's method, the `Dog` class adds its own specific behavior by printing "Dog barks". This way, the subclass extends the functionality of the superclass method while still preserving the original behavior.

When we run the `Main` class and call `dog.makeSound()`, the output will be:
 

Animal makes a sound
Dog barks

As you can see, the `makeSound()` method of the `Dog` class first invokes the `makeSound()` method of the `Animal` class using `super.makeSound()`, and then it executes its own specific behavior.

Using `super` to invoke parent class methods allows for code reuse and enables the subclass to extend or modify the behavior of the superclass methods while still maintaining the inheritance relationship. It provides a way to call the superclass's implementation of a method before or after the subclass's own implementation, allowing for additional functionality or customization in the subclass.

Note: It's important to note that `super` can only be used to invoke methods of the immediate parent class. If you need to invoke methods from higher-level superclasses, you would need to use the appropriate class name instead of `super`.

Super Keyword is used to invoke the parent class constructor

In Java, the `super` keyword is used to invoke the constructor of the immediate parent class from within the constructor of the subclass. When a subclass is created, it is often necessary to initialize the instance variables and perform any required setup specific to the subclass. However, before the subclass constructor can execute, it must first invoke the constructor of its superclass to ensure proper initialization of inherited members.

With the `super` keyword followed by parentheses `()` and any necessary arguments, the subclass constructor can explicitly invoke the constructor of its parent class. This invocation must be the first statement in the subclass constructor.

Let's discuss an example to show the use of `super` to invoke a parent class constructor:

  • Java

Java

class Person {
private String name;

public Person(String name) {
this.name = name;
}
}

class Student extends Person {
private int studentId;

public Student(String name, int studentId) {
super(name); // Invokes the constructor of the Person class
this.studentId = studentId;
}
}

public class Main {
public static void main(String[] args) {
Student student = new Student("John Doe", 12345);
}
}
You can also try this code with Online Java Compiler
Run Code

In this example, we have a superclass `Person` with a constructor that takes a `name` parameter. The subclass `Student` extends the `Person` class and has an additional instance variable `studentId`.

The `Student` class defines its own constructor that takes `name` and `studentId` as parameters. Inside the `Student` constructor, we use `super(name)` to invoke the constructor of the `Person` class, passing the `name` argument. This ensures that the `name` instance variable of the `Person` class is properly initialized.

After invoking the superclass constructor, the `Student` constructor proceeds to initialize its own specific instance variable `studentId`.

When we create an instance of the `Student` class in the `Main` class, the `Student` constructor is called with the provided arguments. The `super(name)` statement within the `Student` constructor invokes the `Person` constructor, initializing the `name` instance variable. Then, the `studentId` instance variable is initialized by the `Student` constructor.

Using `super` to invoke the parent class constructor ensures that the necessary initialization and setup tasks of the superclass are performed before the subclass-specific initialization takes place. It allows for proper initialization of inherited members and helps maintain the integrity of the object's state.

Note: It's important to remember that if a subclass does not explicitly invoke a superclass constructor using `super`, the default constructor (parameterless constructor) of the superclass is automatically invoked. If the superclass does not have a default constructor and the subclass does not explicitly invoke a superclass constructor, a compilation error will occur.

Characteristics of Super Keyword in Java

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 super() in class?

super() is a constructor call used to invoke the parent class's constructor in Java, enabling inheritance and initializing the parent class state.

Why is super() used in Java?

super() is used in Java to call the superclass's constructor, allowing the subclass to inherit and initialize properties or behaviors from the parent class.

What is super and this keyword in Java?

super refers to the superclass, accessing its methods or constructors, while this refers to the current class instance, used to access its members.

Conclusion

In this article, we have discussed Super Keyword in Java. The super keyword is a powerful feature in Java that facilitates interaction between a subclass and its superclass. It not only allows access to inherited methods and variables but also plays a vital role in method overriding, enabling developers to build on existing functionality.

Live masterclass