Say I have the following classes:
class Foo {
protected void method() {}
}
class Bar extends Foo {
}
At this point, from the class Bar, I have access to method()
in two ways:
super.method();
this.method();
From what I see, they seem to perform the same action. Is there a difference between these two in this context? Is there a preferred version to use if so?
Using super
makes sense because method()
is part of the super class. Using this
makes sense too I suppose, as Bar will inherit the properties of the class Foo, and therefore method()
as well, right?
this
unless it is necessary to usesuper
. – Terrorstrickensuper
was possibly preferred if the method isn't overridden, as to point to where it's defined. – Anglinthis
if you must. It hints that there is a reason for doing so, and that reason should be very clear. – Namnama