Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>when i run this code display method of derived class is being called</p> </blockquote> <p>Looks like you have programmed in other programming languages like C++ or C# where you must specify the method as <code>virtual</code> in order to use the subclass method. In Java, all the methods except <code>private</code>, <code>final</code> and <code>static</code> are virtual.</p> <p>Knowing this, since <code>Child</code> class already overrides <code>display</code> method from <code>Parent</code> class, when you do this:</p> <pre><code>Parent a1 = new Child(); a1.display(); </code></pre> <p><code>a1.display</code> will execute the <code>display</code> method of the actual object reference class, in this case, <code>Child#display</code>.</p> <p>In order to <em>fix</em> the code, change the initialization of <code>a1</code> to be a new <code>Parent</code>:</p> <pre><code>Parent a1 = new Parent(); a1.display(); </code></pre> <p>More info:</p> <ul> <li><a href="https://stackoverflow.com/q/4547453/1065197">Can you write virtual functions / methods in Java?</a></li> </ul> <hr> <p>After your edit, now you face the problem when printing <code>a1</code> in the console, probably printing <code>9</code> instead of <code>8</code>. What you're doing here is <strong>hiding the field</strong>. Fields <strong>do not</strong> get overridden, so the value of the <code>i</code> variable will depend on the actual class type declared for the variable. Since you have declared <code>a1</code> as <code>Parent</code>, then <code>a1.i</code> will take the value of <code>Parent#i</code>.</p> <p>In order to <em>fix</em> this code, change the <code>a1</code> variable type to <code>Child</code>:</p> <pre><code>Child a1 = new Child(); System.out.println(a1.i); a1.display(); </code></pre> <p>Or a even better solution: <strong>NEVER</strong> try to <em>override</em> a field, and what's worse, don't try to access to class fields from other classes directly, instead use the respective <strong>getters</strong> and <strong>setters</strong>.</p> <p>More info:</p> <ul> <li><a href="http://docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html" rel="nofollow noreferrer">Hiding fields</a></li> </ul> <hr> <blockquote> <p>How to access methods of ancestor class when intermediate parent class overrides it?</p> </blockquote> <p>Assuming this is your real question, then the answer is simple: you can't.</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.
    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