Java this and super keyword
this keyword in Java :
this is a way for us to reference an object of the class which is being created/referenced.
It is used to call the default constructor of the same class.
this keyword eliminates the confusion between the parameters and the class attributes with the same name. Take a look at the example given below :
In the above example, the expected output is 65 because we've passed x=65 to the constructor of the cwh class. But the compiler fails to differentiate between the parameter 'x' & class attribute 'x.' Therefore, it returns 0.
Now, let's see how we can handle this situation with the help of this keyword. Take a look at the below code :
Now, you can see that we've got the desired output
A reference variable used to refer immediate parent class object.
It can be used to refer immediate parent class instance variable.
It can be used to invoke the parent class method.