Super and This keyword in Java3 min read

In this tutorial, we will learn more about super and this keyword in Java.

Table of Contents

super keyword

  • The “super” keyword is used to access parent class methods, whereas “this” is used to access current class methods.
  • In Java, the keyword super is a reserved keyword, which means we can’t use it as an identifier.
  • The term super is used to refer to the instance of a super-class as well as static members.
  • super is also used to invoke super-class method or constructor.
  • super can be used to call a method from a parent class.
// Program to illustrate super keyword
// refers super-class instance
class Parent{
  // instance variable
  int num1 = 100;
  
  // static variable
  static int num2 = 200;
}
 
class Base extends Parent{
  void fun(){
    
    // referring parent class(i.e, class Parent) instance variable(i.e, a)
    System.out.println(super.num1);
    
    // referring parent class(i.e, class Parent) static variable(i.e, b)
    System.out.println(super.num2);
  }
  
  public static void main(String[] args){
    // Uncomment this and you will see here you will get Compile Time Error since we cannot use 'super' in static context.
    // super.a = 700;
    
    new Base().fun();
  }
}

Output:

100
200

The super keyword can also be used to call a method from a parent class. If the subclass has the same method as the parent class, it should be used. To put it another way, it’s used when a method is overridden.

class Animal{
  void eat(){
    System.out.println("eating...");
  }  
} 

class Cat extends Animal{  
  void eat(){
    System.out.println("eating fish...");
  }  
  
  void purr(){
    System.out.println("purring...");
  }  
  
  void fun(){  
    super.eat();  
    purr();  
  }  
} 

class TestSuper{  
  public static void main(String[] args){  
    Cat cat = new Cat();  
    cat.fun();  
  }
}  

Output:

eating...
purring...

this keyword

Because constructors and setters have parameters with the same names as for instance variables, the “this” keyword is typically used inside constructors and setters, and optionally inside getters (fields).

  • We can’t use this as an identifier because it’s a reserved keyword in Java.
  • This is used to refer to the instance of the current class as well as static members.
  • This can be utilized in a variety of situations, as shown below:
    • to refer to a current class instance variable
    • to invoke or initiate the current class constructor
    • Is a parameter that can be provided as an argument to a method call
    • can be passed as an argument in the constructor call
    • can be used to get the current instance of a class.

The code below demonstrates how to use the “this” keyword.

// Program to illustrate this keyword
// is used to refer current class
class ThisExample{
  // instance variable
  int num1 = 15;
  
  // static variable
  static int num2 = 25;
  
  void funThis(){
    // referring current class(i.e, class ThisExample) instance variable(i.e, num1)
    this.num1 = 100;
    System.out.println(num1);
    
    // referring current class(i.e, class ThisExample) static variable(i.e, num2)
    this.num2 = 500;
    System.out.println(num2);
  }
  
  public static void main(String[] args){
        //Uncomment this and you will see here you will get Compile Time Error since we cannot use "this" in static context.
        // this.num1 = 700;
    
        new ThisExample().funThis();
    }
}

Output:

100
500

Share:

Leave a Reply