Table of contents
1.
Introduction
2.
Why Are They Important in Java Programming?
3.
“Super” Keyword in Java
4.
Uses of super Keyword in Java
5.
Examples of super Keyword in Java
5.1.
Refer To Immediate Superclass Instance Variable
5.2.
Java
5.3.
Invoke The Immediate Superclass Constructor
5.4.
Java
5.5.
Invoke The Immediate Superclass Method
5.6.
Java
6.
“This” Keyword in Java
6.1.
Java
7.
Uses of this Keyword in Java
8.
Examples of this Keyword in Java
8.1.
Refer Current Class Instance Variable 
8.2.
Java
8.3.
Invoke The Current Class Constructor
8.4.
Java
8.5.
Invoke The Current Class Method 
8.6.
Java
8.7.
Return Current Class Instance
8.8.
Java
8.9.
Pass As An Argument In The Method
8.10.
Java
8.11.
Pass As An Argument In The Constructor Call
8.12.
Java
9.
Difference Between this And super Keyword in Java
10.
Real-World Use Cases of super and this
10.1.
1. Calling Overridden Methods
10.2.
2. Avoiding Variable Shadowing
10.3.
3. Managing Inheritance Effectively
11.
Frequently Asked Questions
11.1.
Should I use super or this?
11.2.
Is super() automatically called?
11.3.
Is super() called by default?
11.4.
Can we use both this() and super() in the same constructor?
11.5.
Why this and super cannot be used together?
12.
Conclusion 
Last Updated: Jun 7, 2025
Easy

Super and This Keywords in Java

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

Introduction

In Java, the super and this keywords are important when working with classes and objects. They help manage inheritance and refer class members.

The super keyword is used to refer to the parent class, making it useful for accessing parent methods and constructors. On the other hand, this is used to refer to the current class instance, helping in situations where there are naming conflicts between variables or when calling one constructor from another.

In this blog, we will explore how super and this work, their differences, and how they make Java programming easier.

intro_image

Why Are They Important in Java Programming?

The keywords this and super play a key role in writing clear, maintainable, and reusable Java code.

The this keyword refers to the current object. It helps avoid naming conflicts, especially when method parameters or constructors have the same name as class fields. For example:

this.name = name;

 

This improves code readability and ensures the correct variable is being referenced.

The super keyword refers to the parent class. It's useful for accessing overridden methods, calling superclass constructors, or accessing inherited fields. This is especially helpful in method overriding and constructor chaining:

super.display();
super(name);

 

By using this and super properly, developers can write more structured and reusable code, particularly when working with inheritance and class hierarchies.

“Super” Keyword in Java

In Java, the super keyword is a reference variable that refers to the immediate superclass/parent class objects if using the keyword inside the child class. 

Uses of super Keyword in Java

  • Access Parent Class Variables: When a child class has a variable with the same name as its parent class, super helps access the parent’s variable.
  • Call Parent Class Methods: If a child class overrides a method from its parent, super allows calling the parent class’s version of the method.
  • Invoke Parent Class Constructor: super() can be used to call the parent class constructor from a child class constructor.
  • Differentiate Between Parent and Child Class Members: It helps when both parent and child classes have members with the same name.
  • Improve Code Reusability: By using super, we can reuse parent class functionalities instead of rewriting code in the child class.

Examples of super Keyword in Java

To understand how super works, let's look at a few examples. These examples will show how super helps in accessing parent class methods, variables, and constructors.

Refer To Immediate Superclass Instance Variable

the super keyword is used to access the superclass/ parent class field provided parent and child class have the same fields. 

For example

  • Java

Java

class Father{  
      int age=47; 

class Child extends Father{ 
      int age=20; 
      void displayAge(){ 
            System.out.println(“Age of Child is ”+age);//prints marks of Student1 class 
            System.out.println(“Age of Father is ”+super.age);  //prints marks of Student2 class 
       } 

class Example7{ 
      public static void main(String args[]){ 
            Child c=new Child(); 
            c.displayAge(); 
      }
You can also try this code with Online Java Compiler
Run Code

 

Output

output8

Invoke The Immediate Superclass Constructor

super keyword can be used to invoke the parent class constructor.

For example

  • Java

Java

class Father{  
      Father(){
             System.out.println("Age of Father is 47");
      } 

class Child extends Father{ 
       Child(){ 
              super(); 
              System.out.println("Age of Child is 20"); 
       } 

class Example8{ 
      public static void main(String args[]){ 
             Child c=new Child(); 
      }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

output9

Invoke The Immediate Superclass Method

If the method is overridden, super keyword can be used to invoke an immediate superclass method

For example

  • Java

Java

class Father{  
     void age(){
         System.out.println("Age is 47");
     } 

class Child extends Father{ 
      void age(){
             System.out.println("Age is 20");
      } 
      void display(){ 
             super.age(); 
     } 

class Example9{ 
      public static void main(String args[]){ 
            Child c=new Child(); 
            c.display(); 
      }
You can also try this code with Online Java Compiler
Run Code

 

Output

output10

“This” Keyword in Java

In Javathis keyword is a reference variable in a method or constructor that refers to the current object. 

  • Java

Java

class Example {
   int value;
   Example (int value){
       this.value = value;
       System.out.println("this = " + this);
   }
   public static void main(String[] args) {
       Example demo = new Example (49);
       System.out.println("demo object = " + demo);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

output1

In the example above, we created an object, demo, of the class Example. this keyword of the class is then printed, along with reference to the object demo.

Here, we can see that both demo object and this have the same reference. It implies that this is a reference to the current object.

Uses of this Keyword in Java

  • Referring to Instance Variables: It helps when local and instance variables have the same name by differentiating them.
  • Calling Another Constructor: The this() keyword can be used to call one constructor from another within the same class.
  • Calling Current Class Methods: It allows calling a method of the same class without creating an explicit object.
  • Returning the Current Object: this can be used to return the current class instance from a method.
  • Passing the Current Object as a Parameter: It helps in passing the current object as an argument to another method or constructor.

Examples of this Keyword in Java

To understand the this keyword better, let’s look at a few examples. These examples will demonstrate how this helps in handling variable shadowing, constructor chaining, and method calls.

Refer Current Class Instance Variable 

this keyword is mainly used to differentiate between the formal parameters and instance variables having the same name.

For example

  • Java

Java

class Mobile{  
     String brand; 
     String model; 
     Mobile(String brand,String model){ 
          this.brand=brand; 
          this.model=model; 
     } 
     void show(){
          System.out.println(brand + “ ” + model);
     } 


class Example1{ 
     public static void main(String args[]){ 
           Mobile mob1=new Mobile("Samsung", “GalaxyA23”); 
           Mobile mob2=new Mobile("Apple", “iPhone14”); 
           mob1.show(); 
           mob2.show(); 
     }
}
You can also try this code with Online Java Compiler
Run Code

Output

output2

Invoke The Current Class Constructor

this keyword helps in constructor reusing and accomplishing constructor chaining. 

For example

  • Java

Java

class Demo{  
    Demo(){
         System.out.println("This is a demo constructor");
    }  
    Demo(String temp){ 
         this(); 
         System.out.println(temp); 
    } 

class Example2{ 
    public static void main(String args[]){ 
         Demo obj=new Demo(“Demo for this keyword”); 
    }
You can also try this code with Online Java Compiler
Run Code

 

Output

output3

Invoke The Current Class Method 

For invoking a current class method, this keyword is used. However, if the user does not add this keyword, the compiler automatically adds it. 

For example: 

  • Java

Java

class Calculator{  
     void addition(){
           System.out.println("Addition of 4 and 3 is " + 7);
     } 
     void multiplication(){ 
           System.out.println("Multiplication of 4 and 3 is " + 12);
           this.addition();  // this.addition() and addition() is same
     } 

class Example3{ 
    public static void main(String args[]){ 
           Calculator calc=new Calculator(); 
           calc.multiplication(); 
    }
You can also try this code with Online Java Compiler
Run Code

 

Output

output4

Return Current Class Instance

this keyword can be returned as a statement from the method.

For example

  • Java

Java

class Demo{  
     Demo obj(){ 
        return this; 
     } 
     void display(){
          System.out.println("Coding Ninjas");
     } 

class Example4{ 
   public static void main(String args[]){ 
        new Demo().obj().display(); 
   } 
}
You can also try this code with Online Java Compiler
Run Code

  

Output

output5

Pass As An Argument In The Method

this keyword can also be passed as an argument in the class method. 

For example

  • Java

Java

class Example5{
 void demo(Example5 obj){
      System.out.println("Hello Coding Ninjas");
 }
 void show(){
      demo(this);
 }
 public static void main(String args[]){
    Example5 obj = new Example5();
    obj.show();
 }
}
You can also try this code with Online Java Compiler
Run Code

Output

output6

Pass As An Argument In The Constructor Call

For using one object in many classes, this keyword can be passed in the constructor call. 

For example

  • Java

Java

class Demo{  
     Example6 obj; 
     Demo(Example6 obj){ 
          this.obj=obj; 
     } 
     void display(){ 
          System.out.println(obj.str);  //using data member of Example6 class 
     } 


class Example6{ 
    String str=”Hello! I’m a Ninja”; 
    Example6(){ 
        Demo obj1=new Demo(this); 
        obj1.display(); 
    } 
    public static void main(String args[]){ 
         Example6 obj=new Example6(); 
    } 
You can also try this code with Online Java Compiler
Run Code

Output

output7

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

Difference Between this And super Keyword in Java

this vs super

this 

super

In Java, this keyword is a reference variable that refers to the current object or instance of a class. In Java, super keyword is a reference variable that refers to the superclass's object or instance of a class. 
It can access variables and methods of the current class.It can access variables and methods of the parent class or superclass.
Default constructor of the current class can be invoked by this.Default constructor of the parent class can be invoked by super.
this() can be passed in the method call as an argument.super() is returned with no arguments.
this() can be passed in the method call as an argument.super() can be used to call methods from superclass.

We hope you have learned everything about this and super keywords in Java. 🙌

Also read, Duck Number in Java

Real-World Use Cases of super and this

1. Calling Overridden Methods

The super keyword allows a subclass to call a method defined in its parent class, even if it’s overridden. This is useful when you want to extend the parent’s behavior, not completely replace it.

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

class Dog extends Animal {
    void sound() {
        super.sound(); // Call parent method
        System.out.println("Dog barks");
    }
}

2. Avoiding Variable Shadowing

When a method parameter has the same name as an instance variable, this helps refer to the class field to avoid confusion or errors.

class Person {
    String name;
    Person(String name) {
        this.name = name; // 'this.name' refers to the instance variable
    }
}

3. Managing Inheritance Effectively

super helps call a parent class’s constructor, allowing subclasses to reuse and extend initialization logic. Think of it as inheriting the foundation before adding new floors.

class Vehicle {
    Vehicle(String type) { System.out.println("Vehicle type: " + type); }
}

class Car extends Vehicle {
    Car() {
        super("Car"); // Call Vehicle constructor
        System.out.println("Car created");
    }
}

Frequently Asked Questions

Should I use super or this?

Use super to refer to the parent class and this to refer to the current class. Choose based on whether you need parent or current class members.

Is super() automatically called?

Yes, super() is automatically called in a child class constructor if no other constructor call (super() or this()) is explicitly written.

Is super() called by default?

Yes, if a child class constructor does not explicitly call super() or this(), Java implicitly inserts super() to call the parent class’s no-argument constructor.

Can we use both this() and super() in the same constructor?

No, both this() and super() must be the first statement in a constructor, so they cannot be used together in the same constructor.

Why this and super cannot be used together?

Both this() and super() must be the first statement in a constructor, and Java does not allow two first statements in the same constructor.

Conclusion 

Understanding the super and this keywords in Java is essential for writing clean, efficient, and maintainable code. The super keyword helps in accessing parent class members, calling overridden methods, and invoking parent class constructors, making it useful for working with inheritance. On the other hand, the this keyword is used within the same class to refer to instance variables, call constructors, and pass the current object.

You can refer to similar articles for more information

  1. Fibonacci Series in Java
  2. Inheritance in Java
  3. Introduction to keywords in Java
  4. Constructor chaining in Java
  5. Static and Instance methods in Java
Live masterclass