DEV Community

Cover image for Difference Between this and super in Java
Rahul Raj
Rahul Raj

Posted on

Difference Between this and super in Java

Introduction

I would like to explain the difference between this and super in java with proper examples .

What is this ?

  • this is keyword.

  • this keyword refers current class instance variable and instance method .

  • We have to use this in non-static area only otherwise we get error . This is because this keyword is non-static reference variable .

  • this is by default available in instance block , instance method and constructor After creation of object of the current class .

  • if instance variable and local variable name are same and we want to access instance variable then we have to mention this keyword explicitly.

What issuper?

  • super is also keywords .
  • super keyword refers Super class instance variable and instance method .
  • We have to mention super keyword in non-static area only. If we use anywhere else we get compile time Error .
  • super k/w by default available in instance block , instance method and constructor After creation of object of the sub-class .
  • if super class instance members and sub class instance members having different name then we can access super class's instance members and sub class's instance members without super and this keyword respectively otherwise we have to mention this & super keywords according to our requirement .

codes

// Example 1 :
// Accessing super class & sub class members with and without super & this k/w

class A{
    int y=10;    
}
public class Main extends A{
int x=20;
{
 System.out.println("Access y with super keywords : "+super.y);
System.out.println("Access y without super keywords  : "+y);
 System.out.println("Access x with this keywords : "+this.x);
System.out.println("Access x without this keywords : "+x);
}
    public static void main(String[] args) {
    new Main();  

    }
}

Enter fullscreen mode Exit fullscreen mode

Output :

Access y with super keywords : 10
Access y without super keywords  : 10
Access x with this keywords : 20
Access x without this keywords : 20
Enter fullscreen mode Exit fullscreen mode
//Example 2 :
// super class and sub class having same variable name 

class A{
    int x=10;    
}
public class Main extends A{
int x=20;
{
 System.out.println("access X with super k/w , get super class x value : "+super.x);
 System.out.println("access X with this k/w , get current class x value : "+this.x);
System.out.println("access X without any k/w , get current class x value : "+x);
}
    public static void main(String[] args) {
    new Main();  

    }
}

Enter fullscreen mode Exit fullscreen mode

Output :

access X with super k/w , get super class x value : 10
access X with this k/w , get current class x value : 20
access X without  k/w , get current class x value : 20
Enter fullscreen mode Exit fullscreen mode
//Example 3 :
//Accessing super class & sub class method having same name  with and without super & this k/w

class A{
    void display(){
        IO.println("display() method inside super class , have to call with super k/w ");
    }    
}
public class Main extends A{
void display(){
        IO.println("display() method inside sub class , need to call with/without this k/w");
    }
{
 super.display();//call A-class method 
 this.display();// call current class method
 display();// also call current class method
}
    public static void main(String[] args) {
    new Main();  

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

display() method inside super class , have to call with super k/w
display() method inside sub class , need to call with/without this k/w
display() method inside sub class , need to call with/without this k/w
Enter fullscreen mode Exit fullscreen mode

In Short :

  • this keyword refers current class instance members after creation of sub class's object.
  • super keyword refers super class instance members after creation of sub class's object.
  • Both this & super ketwords are non-static object reference variable . So we have to mention this & super keywords in non-static areas only .

Top comments (0)