Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The keyword <code>super</code> doesn't "stick". Every method call is handled individually, so even if you got to <code>SuperClass.method1()</code> by calling <code>super</code>, that doesn't influence any other method call that you might make in the future.</p> <p>That means there is no direct way to call <code>SuperClass.method2()</code> from <code>SuperClass.method1()</code> without going though <code>SubClass.method2()</code> unless you're working with an actual instance of <code>SuperClass</code>.</p> <p>You can't even achieve the desired effect using Reflection (see <a href="http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html#invoke%28java.lang.Object%2C%20java.lang.Object...%29" rel="noreferrer">the documentation of <code>java.lang.reflect.Method.invoke(Object, Object...)</code></a>).</p> <p><strong>[EDIT]</strong> There still seems to be some confusion. Let me try a different explanation.</p> <p>When you invoke <code>foo()</code>, you actually invoke <code>this.foo()</code>. Java simply lets you omit the <code>this</code>. In the example in the question, the type of <code>this</code> is <code>SubClass</code>.</p> <p>So when Java executes the code in <code>SuperClass.method1()</code>, it eventually arrives at <code>this.method2();</code></p> <p>Using <code>super</code> doesn't change the instance pointed to by <code>this</code>. So the call goes to <code>SubClass.method2()</code> since <code>this</code> is of type <code>SubClass</code>.</p> <p>Maybe it's easier to understand when you imagine that Java passes <code>this</code> as a hidden first parameter:</p> <pre><code>public class SuperClass { public void method1(SuperClass this) { System.out.println("superclass method1"); this.method2(this); // &lt;--- 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); } } </code></pre> <p>If you follow the call stack, you can see that <code>this</code> never changes, it's always the instance created in <code>main()</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload