Reputation: 14449
Are there any reason to call method from super class?
I have met lots of places where super
method called instead of this
method, e.g.:
public String getCustomValue() {
String value = (String) super.getValue(someArgHere);
return value;
}
Any benefits? I just see one major problem with inheritance: if I override getValue
in this class or in one of its descendants getCustomValue
will neglect that override and call super
method.
Upvotes: 2
Views: 634
Reputation: 6325
Unless you overwrote the method getValue(...)
and you are really sure (your sureness deserves a comment in the code) you want to bypass it you should not use super
like you are doing. Later on if you or someone else decide to overwrite getValue(...)
they probably wanted the effect to apply to getCustomValue()
as well.
Although you definitely can call super.myMethodOne()
from myMethodTwo()
, the usual scenario is when you want to call super.myMethodOne()
from myMethodOne()
when you override it. Some languages like Ruby even pass up the method arguments automatically for you so that you don't have to retype them.
Here is one example:
public class A {
public void close() {
// do a bunch of things...
}
}
public class B extends A {
@Override
public void close() {
// close things related to the subclass B
// and then make sure A is closed as usual...
super.close();
}
}
Upvotes: 2
Reputation: 129547
I just see one major problem with inheritance: if I override
getValue
in this class or in one of its descendantsgetCustomValue
will neglect that override and call super method.
Then don't call the super
method explicitly, just call getValue
. If the method has not been overriden it will default to the super-method. If it has, it will use the overriden method.
I don't know if it's appropriate to ask about "benefits" in this case - it really just depends on what exactly you are trying to accomplish.
Upvotes: 1
Reputation: 31744
The thing is the design. When we code, we do it as per what it is!
So even if getValue
is extended, its perfect, because that is what your class is suppose to do.
Normally, super
is used, to obtain any information or data or side effect from the super type
and modify or improve it as per your current class functionality
Upvotes: 0
Reputation: 4264
super.getValue(someArgHere)
calls the getValue
method of the super class. In contrast, this.getValue(someArgHere)
calls the getValue
method of the current class, if defined. If the current class does not override getValue
, the super class method is called.
Upvotes: 2
Reputation: 26520
There are no technical advantages of using super
over this
in the case where the method is not overridden.
However, one might say that it's clearer to use super
instead of this
for the reason you've just mentioned. If you override the function in your subclass, then you will need to use super
; if you don't you can use this
. Instead of playing guessing games (or forcing people to check whether the method has been overridden), you can just always use super
when you mean super
.
Upvotes: 1