Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>super</code> is a keyword allowing you to call the method implementation defined in the superclass. It is not a field of your sub-class.</p> <blockquote> <p>If it is not, where are overrided method hold?</p> </blockquote> <p>I'm not quite sure what you mean by this, but:</p> <ul> <li>the method which prints "Super" is held in the class definition of the superclass</li> <li>the method which prints "Sub" is held in the class definition of the subclass.</li> </ul> <p>Since <code>Sub</code> extends <code>Super</code>, the definition of the <code>Sub</code> class includes a reference to the definition of the <code>Super</code> class.</p> <p>Answering updated questions:</p> <blockquote> <p>When we are calling method with super, you say, we are acessing parent's method. But how can we call this method without parent's object?</p> </blockquote> <p>A method is just a block of code, just a sequence of bytecode instructions that we need to execute. When you invoke a method, the JVM's task is to determine, from the method name and parameters you give, where to find this block of code. Normally, as others have said, it will first look in the class definition of the class of the object on which the method was invoked. When you use <code>super</code>, you are telling the JVM not to look here, and instead look in the parent class definition.</p> <p>So you don't need separate instances of <code>Super</code> and <code>Sub</code>, because a <code>Sub</code> <em>is</em> a <code>Super</code> (<code>new Sub() instanceof Super</code> is <code>true</code>), and because the JVM knows that the <code>super</code> keyword means that it should look for the code composing a method in the class definition of <code>Super</code>.</p> <blockquote> <p>Is super same as this? this is a reference to concrete object, as you know.</p> </blockquote> <p>No, they're not the same. <code>this</code> is a reference to the current object, whereas <code>super</code> is not a reference to an object, instead it is a keyword which affects where the JVM will look for the code defining a method which is being invoked.</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