Reputation: 55
In oracle tutorial, it says: If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.
It mentioned about the use of super in overridden method.
However, I reality, in the sample program I wrote, I can use super keywords to access any method in the superclass.
The question here is: why most people talking online about the use of super, they always talking about the invocation of overridding method?
I mean, why that "use super to call other method in superclass" is not recommended?
BTW, one more question here: we can't use super in static method. The compiler won't let us do that.
Is it because the variable "super" belongs to an object instead of the class, just like keyword "this"?. A static method belongs to a class, and the static method doesn't have the variable "super"?
Upvotes: 4
Views: 570
Reputation: 109532
super.f()
bypasses this.f()
. If there is no implementation this.f()
then super
is not needed. Otherwise there is a bit of code smell when not called inside this.f()
.
In short: one cannot find many sensible usages for g()
calling super.f()
: it provides no solution for any known problem.
I could only think of the following: a layouting of rectangles, by using recursively horizontally listed boxes, every box inside listing vertically listed boxes, inside listing horizontally listed boxes etc.
public class SpecialBoxLayout extends BoxLayout {
@Override
public void layoutHor(...) {
... // Something extra, special
for
super.layoutVert(...);
...
}
@Override
public void layoutVert(...) {
...
for
super.layoutHor(...);
...
}
This needs inheritance, with a method getting extra functionality, and having complementary methods. A language feature not really needed. Like the non-existing super.super.f()
.
It (g calling super.f) is bad style because:
layout(Orientation.VERT, ...)
.Upvotes: 1
Reputation: 17226
It suggests the old method is still needed (and does something different from the child version of the method), in which case you probably shouldn't be overriding it in the first place. The parent method still "makes sense" within the child object.
Two identically named methods both being used within the same object but with different effects sounds like a nightmare for readability. Given that both are being used it seems highly likely that they should be separate methods with different method names making their subtle difference clear.
This semantically is a very odd thing to write. A parents (non private) methods are the childs methods unless overridden, using super.method()
has an identical effect to method()
and so it is not surprising you have not seen this mentioned. As you have supposed it is also very likely to cause bugs in the future if you later do override the method.
Similarly to this you can put public
on the front of interface methods but it has no effect.
Static methods cannot be overridden, if a parent class and a child class happen to have the same method name that is simply a coincidence as far as the compiler is concerned (although it does hide the static method in the parent class - it does not override it). Given that a static method cannot be overridden using the super
keyword to access one becomes meaningless. Furthermore you can access a static method in Foo as
Foo.staticMethod();
Upvotes: 6
Reputation: 509
Assume that you have classes A and B, where B extends A. Class A has methods: methodOne(), methodTwo() and class B overrides methodOne(). However remember that class B has also methodTwo(), since it's inherited so there is no use to call it via super because you have it directly in descendant class.
EDIT Let's take a look on that:
import java.util.*;
class A {
public void methodOne() {
System.out.println("A.1");
methodTwo();
//super.methodTwo();
/*This above would lead to compilation error even though
it would work as intended - always print A.2.
Use instead for instance: ((A)this).methodTwo();*/
}
public void methodTwo() {
System.out.println("A.2");
}
}
class B extends A{
@Override
public void methodTwo() {
System.out.println("B.2");
}
public B() {
methodOne();
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.methodTwo();
b.methodTwo();
}
}
Upvotes: 0
Reputation: 718678
However, I reality, in the sample program I wrote, I can use super keywords to access any method in superclass.
In the real reality you can't ... except in very specific circumstances.
You can't use super
to call a method that is forbidden by the access rules:
Calling a private
method is forbidden unless this class is nested within the other or vice versa.
Calling a package private method is forbidden unless this class is in the same package as the other.
Calling a protected
method is allowed.
If you have an example that appears to contradict this, please add it to your Question so that we can explain what is actually going on.
The question here is: why most people talking online about the use of super, they always talking about the invocation of overridding method?
I think you may be talking about this:
public class A {
public void foo() {...}
}
public class B extends A {
public void foo() { // The overriding method.
...
super.foo(); // invoking the overridden method.
...
}
}
The method in the child class invokes the overridden version of itself. The reason that you are seeing it in lots of example is that it is the most common use-case for super
. That's all.
On the other hand if the method A.foo()
is not overridden at all, then it is legal to use super.foo()
to call it in B
... but I don't think it is good style. For a start, it would probably break if you then did add an override method in B
.
Upvotes: 2
Reputation: 13596
You can use super.method()
to call any method, but you can use super()
only to call the overridden method.
Upvotes: 0