Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay. Let's go, line by line through your code.</p> <p>Your first statement, in your 'foo' method is</p> <pre><code>super.foo(); </code></pre> <p>Well that's an explicit call to the superclass <code>foo</code> method. Which is:</p> <pre><code> void foo() { System.out.println("Super"); } </code></pre> <p>So "Super" is outputted to the console, becuase you've explicitly called this method with the <code>super</code> keyword. <code>super</code> refers to the class' superclass, in the same way that <code>this</code> refers to the current class.</p> <p>Next is the rest of the <code>foo</code> method in the subclass:</p> <pre><code> void foo() { super.foo(); System.out.println("Sub"); } </code></pre> <p>Well after <code>super.foo()</code> is called, it's time to move to the next statement, which outputs "Sub". </p> <hr> <p>The reason why your program moves to the subclass' method first, instead of the superclass, is because of a principle called <code>Polymorphism</code>. That is, a subclass takes a method from the superclass, and alters it's behavior.</p> <p><b>Abstract Classes</b></p> <p>You can't create instances of Abstract classes, no, but with the <code>super</code> keyword, you can access the functionality of the superclass nonetheless.</p> <p><b>In context of the Java Virtual Machine</b></p> <p>So what happens, when you make a method call, is the Java Virtual Machine will look for the method in the local class, if it is an instance method. If it can not find it, it will move to the superclass. When you use the principle of <code>Polymorphism</code>, the JVM finds the method with the correct signature in the subclass, and stops looking. This is how inheritance and <code>Polymorphism</code> works in simple terms, in the context of Java.</p> <p>When you override a method, you add a method with the same method signature (the name, number and type of a method's fields) to the subclass definition. This is how the JVM finds it, and this is where the overridden method is stored.</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. 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.
 

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