Reputation: 855
In the following example pseudocode:
public class MyPanel extends JPanel {
public void reset() {
this.clear();
clear();
}
public void clear() { System.out.println("FAIL"); }
};
public class MySpecialPanel extends MyPanel {
public void clear() { System.out.println("Hello, world"); }
};
When calling (new MySpecialPanel()).reset()
shouldn't both this.clear()
and clear()
resolve in the same scope? Is there any difference between this.clear()
and clear()
?
Upvotes: 0
Views: 741
Reputation: 20783
Stating the obvious as many have already responded - when you call clear()
on an object, that object is in scope; when you use this
you are referring to that same object in context.
Upvotes: 0
Reputation: 306
in your code:
public void reset() {
this.clear();
clear();
}
both this.clear() and clear() are the same.
this is a keyword for setting the scope of the call inside the class itself super sets the scope to the class itself and the superclass
Upvotes: 0
Reputation: 24910
There is no difference between this.clear()
and clear()
.
You have a MySpecialPanel
object and the clear()
method on that object is called twice. To call the superclass's clear, you must use super.clear()
So, you do something like this --
public void reset() {
clear();
super.clear();
}
Upvotes: 1
Reputation: 44706
public void reset() {
this.clear();
clear();
}
In the code above, that calls the same method twice. There is no difference between clear()
and this.clear()
.
You can explicitly call the superclasses method with super.clear()
.
Upvotes: 5