Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
This Keyword in Java
2.1.
How Is this Keyword Used? 
2.1.1.
1️⃣Refer Current Class Instance Variable 
2.1.2.
2️⃣Invoke The Current Class Constructor
2.1.3.
3️⃣Invoke The Current Class Method 
2.1.4.
4️⃣Return Current Class Instance
2.1.5.
5️⃣Pass As An Argument In The Method
2.1.6.
6️⃣Pass As An Argument In The Constructor Call
3.
Super Keyword in Java
3.1.
How is the super keyword used?
3.1.1.
1️⃣ Refer To Immediate Superclass Instance Variable
3.1.2.
2️⃣ Invoke The Immediate Superclass Constructor
3.1.3.
3️⃣Invoke The Immediate Superclass Method
4.
Difference Between this And super💠
5.
Frequently Asked Questions
5.1.
Can we access parent class variables in the child class by using the super keyword?
5.2.
Can we use both this and super in the constructor?
5.3.
Can we use the super keyword in the static method of a child class for calling the parent class/superclass method?
5.4.
What will happen if this() constructor call is present in the last line of the constructor?
5.5.
Can we refer to static variables using this keyword?
6.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Difference Between this and super in Java

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Java has reserved keywords like this and super, which are reference variables used in different contexts. To understand the difference between this and super in Java, it is essential to know the features of these keywords.

intro_image

In this article, we will study this and super keywords and the differences between the two.

This Keyword in Java

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

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);
    }
}

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.

How Is this Keyword Used? 

this keyword can be used to:

1️⃣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

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();  
      }
} 

Output

output2

2️⃣Invoke The Current Class Constructor

this keyword helps in constructor reusing and accomplishing constructor chaining. 

For example

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”);  
     }
}  

Output

output3

3️⃣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: 

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();  
     }
}  

Output

output4

4️⃣Return Current Class Instance

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

For example

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();  
    }  
}  

Output

output5

5️⃣Pass As An Argument In The Method

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

For example

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();
  }
}

Output

output6

6️⃣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

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();  
     }  
}  

Output

output7

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

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

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. 

How is the super keyword used?

super keyword can be used to:

1️⃣ 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

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();  
       }
}  

Output

output8

2️⃣ Invoke The Immediate Superclass Constructor

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

For example

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();  
       }
} 

Output

output9

3️⃣Invoke The Immediate Superclass Method

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

For example

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();  
       }
}  

Output

output10

Difference Between this And super💠

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

Frequently Asked Questions

Can we access parent class variables in the child class by using the super keyword?

Yes, We can access parent class variables in child class by using the super keyword. 

Can we use both this and super in the constructor?

No, we cannot use both this and super keywords together in the constructor because both the keywords should be the first statement in any Java constructor.

Can we use the super keyword in the static method of a child class for calling the parent class/superclass method?

No, we cannot use super keyword in static methods because it belongs to the immediate parent class or superclass object and static belongs to the class level.

What will happen if this() constructor call is present in the last line of the constructor?

It will throw a compile-time error stating “Constructor call must be the first statement in a constructor.” 

Can we refer to static variables using this keyword?

It is possible to refer to a static variable using this keyword. But one should limit its usage as a static variable belongs to a class, and it needs to be accessed in a static way.

Conclusion 

In this blog, we studied Java's this and super keywords. We further explored the difference between the two keywords.

We hope this blog has helped you enhance your knowledge of this and super keywords in Java and if you want to learn more, check out our articles here

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

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Also, look at the interview experiences for placement preparations. 

Happy Learning Ninja! 

Previous article
How to Run Java Program?
Next article
ClassLoader in java
Live masterclass