Reputation: 920
I saw some Java examples that using this
keyword to get superclass methods. Example: this.superClassMethod()
. In usual case we would use super
. Could someone help to clarify with example why the developer used this
instead of super
? Thank you.
Upvotes: 8
Views: 8002
Reputation: 48817
There is no difference between this.method()
and super.method()
until the said method()
gets overridden in the caller's class.
For example, with
class SuperClass {
public void method() {
System.out.println("SuperClass");
}
}
class SubClass extends SuperClass {
public SubClass() {
method();
this.method();
super.method();
}
}
Calling
new SubClass();
Prints
SuperClass
SuperClass
SuperClass
While with
class SuperClass {
public void method() {
System.out.println("SuperClass");
}
}
class SubClass extends SuperClass {
@Override
public void method() {
System.out.println("SubClass");
}
public SubClass() {
method();
this.method();
super.method();
}
}
Calling
new SubClass();
Prints
SubClass
SubClass
SuperClass
In parallel, there is no difference between this.field
and super.field
until the said field
gets hidden in the caller's class.
For example, with
class SuperClass {
protected String field = "SuperClass";
}
class SubClass extends SuperClass {
public SubClass(String field) {
System.out.println(field);
System.out.println(this.field);
System.out.println(super.field);
}
}
Calling
new SubClass("parameter");
Prints
parameter
SuperClass
SuperClass
While with
class SuperClass {
protected String field = "SuperClass";
}
class SubClass extends SuperClass {
private String field = "SubClass";
public SubClass(String field) {
System.out.println(field);
System.out.println(this.field);
System.out.println(super.field);
}
}
Calling
new SubClass("parameter");
Prints
parameter
SubClass
SuperClass
Side note: methods()
get overriden while fields
get hidden.
Upvotes: 21
Reputation: 4598
A lot of the oracle/sun java code uses this. when it does not need to example see java\awt\Event.java #translate
public void translate(int dx, int dy) {
this.x += dx;
this.y +=
}
(if you extract src.zip and search for this. in *.java will see many instances like this where the there is no overriding local variable/ param but still they use this style.
Nothing to do with params or auto completion (its just x) I think its a coding standard to make it clear that we are talking about a method or field in current/ this class.
Method of base class :Even if not yet over ridden in our class, it can be at a later date, so its future ready. Till the method is overridden it works fine, and if it is over ridden its accurate.
So I think its a best practice coding convention for clarity. It wont bloat the code as not writing is only syntax sugar and the compiler will add it in the class code.
Upvotes: 0
Reputation: 5841
super is used to access methods of the base class, while this is used to access methods of the current class.
Few references
1) usage of this
2) critique on super on SO
Extending the notion, if you write super(), it refers to constructor of the base class, and if you write this(), it refers to the constructor of the very class where you are writing this code.
class Animal {
void eat() {
System.out.println("animal : eat");
}
}
class Dog extends Animal {
void eat() {
System.out.println("dog : eat");
}
void anotherEat() {
super.eat();
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
a.eat();
Dog d = new Dog();
d.eat();
d.anotherEat();
}
}
The output is going to be
animal : eat
dog : eat
animal : eat
The third line is printing "animal:eat" because we are calling super.eat(). If we called this.eat(), it would have printed as "dog:eat".
Upvotes: 3
Reputation: 56697
Using this
does not invoke the superclass method. It is actually superfluous, because it specifically invokes this instance's method. It may be relevant if you want to call another constructor of the same instance, but otherwise it's the same as just calling the method.
It may be useful for variable scoping (for example when there's a local variable with the same name as an instance variable) to make sure the instance variable is used, but it makes no difference when calling methods.
Personally I'd think that the developer wanted to take advantage of code completion and the IDE shows possible method names after entering this.
:-)
Upvotes: 5