Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The child class does not maintain any special field representing its parent. You may be thinking something along the lines of inner classes, which <strong><em>do</em></strong> maintain a reference to their outer class. This is a special case and does not represent how super-sub classes relate to each other. </p> <p>Internally, the JVM maintains a 'method table', associating each class its loaded with the methods available for that class. The JVM also knows about the relationships between all classes its loaded, including super-sub relationships.</p> <p>When you invoke a <code>super</code> function, the JVM actually does a couple of things:</p> <ul> <li>Determines the parent of the class that the method is is being called from</li> <li>Determines the method on the parent that will be called</li> <li>Invokes the method, with a special instruction (<code>invokespecial</code>)</li> </ul> <p>If you were to examine the class file for your <code>Sub</code> class, you would see something like this for the <code>foo</code> method:</p> <pre><code>void foo(); flags: Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #2 // Method Super.foo:()V 4: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 7: ldc #4 // String Sub 9: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V </code></pre> <p><em>Line 1 in the listing shows the special instruction that invokes the superclass method.</em></p> <p>A good source of reading would be the <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/index.html" rel="nofollow">Java Virtual Machine Specification</a>, particularly <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.11.8" rel="nofollow">Section 2.11.8</a>.</p>
 

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