Use of ‘this’ and ‘super’ keywords in java


this keyword:

Suppose the variable within the class and the parameter passing to the method within the same class are the same. If we try to store a value to the class level variable from within that method then we will use ‘this’ keyword as below.

Class  Car {
String color;
public void blueCar(String color)
{
this.color=color;
}
public String getColor()
{
return  this.color ;       //
we can use return color;  as there is no color parameter in this method
}
public String setColor(String color)
{
this.color=color;
}
}

super keyword:

When we try to call the variables or methods of the parent class then we use super keywords. 
As in the below case are calling the exampleMethod of the parent class from a method with the same name (eg.- exampleMethod) within the SubClass section.
If we do not use the super keyword in this case then it will call the method  itself until the memory is full.

class SuperClass{
public void exampleMethod()
{
System.out.println(“Hi, I am  the method within the superclass”);
}
}


class SubClass extends SuperClass{

@override
public void exampleMethod()
{
super.exampleMethod();
}

Class Main
{
public static void main(String[] args)
{
SubClass s= new subClass();
s.exampleMethod();
}
}