Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Now my question is if i am creating an object of the SubClass does it create an object of the SuperClass If yes why..??</p> </blockquote> <p>No. it's not. </p> <p>When you do </p> <pre><code>SubClass sub=new SubClass(); </code></pre> <p>only <code>SubClass</code> object created.And only constructor of <strong>super</strong> class calls. And that calling to super class constructor chained untill the reaches to <code>java.lang.Object</code> constructor.</p> <p><strong>Side note:</strong> A thumb of rule(don't know it is official or not, <em>with each <strong>new</strong> ,only one object creates</em>)</p> <p>You refer :<a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.9.4" rel="nofollow">15.9.4 Run-time Evaluation of Class Instance Creation Expressions</a></p> <blockquote> <p>The new object contains new instances of all the fields declared in the specified class type and all its superclasses. As each new field instance is created, it is initialized to its default value (§4.12.5).</p> </blockquote> <p>So you can come to a conclusion like when <code>class SubClass extends SuperClass</code>, then methods,variables are copied to class <code>SubClass</code> .</p> <p><strong>Edit:</strong></p> <blockquote> <p>how can a constructor be called if an object is not created..??</p> </blockquote> <p>Aah...your doubt making sense to me.</p> <p>But <code>constructor</code> is very special method in java. You can decompile a class file ,you can see the constructors in the name of <code>&lt;init&gt;.</code> You cannot call <strong>explicitly</strong> except with keyword <code>new</code> or <code>super</code>.</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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