Having the following superclass:
public class SuperClass {
protected Integer a;
protected Integer b;
public void doSomething() {
this.a = 10;
}
public void doEverything() {
SuperClass.this.doSomething();
this.b = 20;
}
public static void main(String[] args) {
SuperClass instance = new SubClass();
instance.doEverything();
System.out.println(instance.a); //should print 10
System.out.println(instance.b);
}
}
And the following subclass:
public class SubClass extends SuperClass {
@Override
public void doSomething() {
super.doSomething();
super.a *= 10;
}
public void doEverything() {
super.doEverything();
this.b += 5;
}
}
Outputs:
100
25
So, SuperClass.this.doSomething();
is accessing SubClass
's doSomething
, but I need it to access the SuperClass
's doSomething
. In that case, I don't have the super
keyword, because I'm already on super!
Is there a way to reference¹ the deep
SuperClass.this.doSomething
, so it would output10
for the first output value?¹ I'm interested on referencing: we could, of course, extract the
SuperClass
'sdoSomething
to an auxiliar private method and access it directly.If there is no way, does the situation where a superclass method needing to access its another (although overridden) method mean that my OOP design isn't correct? Why?