Java: Calling a super method which calls an overridden method Java: Calling a super method which calls an overridden method java java

Java: Calling a super method which calls an overridden method


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().


You can only access overridden methods in the overriding methods (or in other methods of the overriding class).

So: either don't override method2() or call super.method2() inside the overridden version.


You're using the this keyword which actually refers to the "currently running instance of the object you're using", that is, you're invoking this.method2(); on your superclass, that is, it will call the method2() on the object you're using, which is the SubClass.