public class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public class SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(); } }
my expected output:
subclass method1
superclass method1
superclass method2
actual output:
subclass method1
superclass method1
subclass method2
I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn’t happening. Any ideas as to how I can make it happen?
Advertisement
Answer
The keyword super
doesn’t “stick”. Every method call is handled individually, so even if you got to SuperClass.method1()
by calling super
, that doesn’t influence any other method call that you might make in the future.
That means there is no direct way to call SuperClass.method2()
from SuperClass.method1()
without going though SubClass.method2()
unless you’re working with an actual instance of SuperClass
.
You can’t even achieve the desired effect using Reflection (see the documentation of java.lang.reflect.Method.invoke(Object, Object...)
).
[EDIT] There still seems to be some confusion. Let me try a different explanation.
When you invoke foo()
, you actually invoke this.foo()
. Java simply lets you omit the this
. In the example in the question, the type of this
is SubClass
.
So when Java executes the code in SuperClass.method1()
, it eventually arrives at this.method2();
Using super
doesn’t change the instance pointed to by this
. So the call goes to SubClass.method2()
since this
is of type SubClass
.
Maybe it’s easier to understand when you imagine that Java passes this
as a hidden first parameter:
public class SuperClass { public void method1(SuperClass this) { System.out.println("superclass method1"); this.method2(this); // <--- this == mSubClass } public void method2(SuperClass this) { System.out.println("superclass method2"); } } public class SubClass extends SuperClass { @Override public void method1(SubClass this) { System.out.println("subclass method1"); super.method1(this); } @Override public void method2(SubClass this) { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(mSubClass); } }
If you follow the call stack, you can see that this
never changes, it’s always the instance created in main()
.