In all the super
keyword tutorials I found online, it's hard to get any examples closer to following one.
My question:
What's the difference between
Tracker.super.track(event);
andtest.parent.Tracker.track(event);
?Why would the first work?
What does
Tracker.super
refers to? A object or a class?
subclass:
package test;
public class Tracker extends test.parent.Tracker {
@Override
public void track(final Event event) {
Executor.execute(new Runnable() {
public void run() {
Tracker.super.track(event); //this works!! why??
super.track(event); // compile error: cannot resolve
test.parent.Tracker.track(event); //compile error: can only reference static method
}
});
}
}
super class
package test.parent;
public abstract class Tracker {
public void track(Event event) {}
}
Reference Updates:
In jls8, 15.11.2
"Suppose that a field access expression T.super.f appears within class C, and the immediate superclass of the class denoted by T is a class whose fully qualified name is S. If f in S is accessible from C, then T.super.f is treated as if it had been the expression this.f in the body of class S. Otherwise, a compile-time error occurs.
Thus, T.super.f can access the field f that is accessible in class S, even if that field is hidden by a declaration of a field f in class T.
It is a compile-time error if the current class is not an inner class of class T or T itself."