Note that there are some explanatory texts on larger screens.

plurals
  1. POInheritance and Object creation, Theoretically and in Real
    primarykey
    data
    text
    <p>Lets say I have a class <code>A.java</code>,</p> <p><img src="https://i.stack.imgur.com/VpU4r.png" alt="enter image description here"></p> <p>When I will execute a constructor method of A, it will create a memory space for <code>xyz</code> Object.</p> <pre><code>A xyz = new A(); </code></pre> <p>The reference to memory may be something like, </p> <pre><code>[xyz] ---&gt; '0x34524' </code></pre> <p>Thats basics of OOP. Simple enough!</p> <h3>Now,</h3> <p>What happens if a class is inheriting from different parent classes? How many object space will be created in memory?</p> <p>Lets say we have,</p> <p><img src="https://i.stack.imgur.com/saRzP.png" alt="enter image description here"></p> <p>and then we create an object of class <code>D.java</code>,</p> <pre><code>D omg = new D(); </code></pre> <p>Here as we know that D's object will call construct of C.java and so on until A.java. Does this mean that in memory we are having 4 different memory reference, because we are instantiating all of the four objects (one directly and another 3 indirectly)?</p> <pre><code>[omg] ---&gt; '0x34525' [C] ---&gt; '0x34526' [B] ---&gt; '0x34527' [A] ---&gt; '0x34528' </code></pre> <hr /> <p><strong>Note :</strong> </p> <ol> <li>This isn't homework question, this is just a curiosity question.</li> <li>I am aware of the fact that if we have a instance variable inside an A.java then we will not create only object A but we will be creating other internal object whenever we hit <code>new</code> keyword.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    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