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.
It is used to call properties and methods of the superclass. It does not call the method, whereas when we create an instance of subclass than that of the parent class is created implicitly so super keyword calls that instance. It can be use to access the data members of parent class when both parent and child have member with same name.
Use of super with methods: This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. This code snippet helps to understand the said usage of super keyword.
Use of super with constructors: super keyword can also be used to access the parent class constructor. One more important thing is that, ‘’super’ can call both parametric as well as non parametric constructors depending upon the situation. Following is the code snippet to explain the above concept:
There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object. Here is given the 6 usage of java this keyword. this can be used to refer current class instance variable. this () can be used to invoke current class constructor. this can be passed as an argument in the method call.
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.
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.
:-)
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With