Super vs This keyword
Super keyword
- Used to access methods of the parent class.
- Represents a current instance of parent class.
- Used to call the default constructor of the parent class.
Example :
class Easy
{
int a = 40;
static int b = 50;
void notes()
{
this.a = 200;
System.out.println(a);
this.b = 500;
System.out.println(b);
this.a = 600;
System.out.println(a);
}
public static void main(String[] args)
{
new Easy().notes();
}
}
Output
200
500
600
This Keyword
- Reserved keyword
- Represents a current instance of class.
- This is used to access methods of the current class.
- Can’t use as an identifier.
- Refer's as static members.
- Used to call the default constructor of the same class.
Example :
class Easy
{
int a = 20;
static int b = 40;
void notes()
{
this.a = 300;
System.out.println(a);
this.b = 700;
System.out.println(b);
}
public static void main(String[] args)
{
new Easy().notes();
}
}
Output
300
0 Comments