Usage of the this and super keywords
The this
keyword provides a reference to the current object. The super
keyword refers to the parent class object. These keywords allow us to refer to a variable or method that has the same name in the current context and the parent object.
Usage of the this keyword
Here is the most popular example:
class A {
private int count;
public void setCount(int count) {
count = count; // 1
}
public int getCount(){
return count; // 2
}
}
The first line looks ambiguous, but, in fact, it is not – the local variable, int count
, hides the int count
private property instance. We can demonstrate this...