Introduction
I would like to explain the difference between this and super in java with proper examples .
What is this ?
thisis keyword.thiskeyword refers current class instance variable and instance method .We have to use
thisin non-static area only otherwise we get error . This is because this keyword is non-static reference variable .thisis 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
thiskeyword explicitly.
What issuper?
-
superis also keywords . -
superkeyword refers Super class instance variable and instance method . - We have to mention
superkeyword in non-static area only. If we use anywhere else we get compile time Error . -
superk/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
superandthiskeyword respectively otherwise we have to mentionthis&superkeywords 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();
}
}
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
//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();
}
}
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
//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();
}
}
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
In Short :
-
thiskeyword refers current class instance members after creation of sub class's object. -
superkeyword refers super class instance members after creation of sub class's object. - Both
this&superketwords are non-static object reference variable . So we have to mentionthis&superkeywords in non-static areas only .
Top comments (0)