Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No new objects are created in the system when you use explicit casting (except in your last case, where you cast a primitive type to an <a href="http://en.wikipedia.org/wiki/Primitive_wrapper_class" rel="nofollow noreferrer">object wrapper</a>, since <code>double</code> is not an object like <code>Double</code> is). Note that this explicit cast isn't necessary due to Java's <a href="http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html" rel="nofollow noreferrer">autoboxing feature</a>.</p> <p>In your <code>(Child) new Object()</code> scenario, you will receive a <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassCastException.html" rel="nofollow noreferrer">ClassCastException</a> because an <code>Object</code> is not a <code>Child</code> (although the opposite is true).</p> <p>The answer to your first scenario is the most complicated. Essentially the parent class is treated like an interface might be. When you cast the <code>Child</code> to the <code>Parent</code>, only the <code>Parent</code> API is available. However, <a href="http://docs.oracle.com/javase/tutorial/java/IandI/override.html" rel="nofollow noreferrer">the overridden method will still be called</a>. So, if you do:</p> <pre><code>Parent p = (Parent) new Child(); p.a(); </code></pre> <p>... the <code>Child</code>'s <code>public void a()</code> will be called, even though it is being seen through the lens of the <code>Parent</code> class. However if you were to have a second method in the <code>Child</code> that the <code>Parent</code> does not have (let's say <code>public void b()</code> for instance), you would <em>not</em> be able to call that without casting the object back to a <code>Child</code>.</p> <p>"Behind the scenes", as you say, the only new thing that is created is another <a href="https://stackoverflow.com/questions/40480/is-java-pass-by-reference">object reference</a> which points to the same object. You can have as many references as you like to the same, singular object. Consider this example:</p> <pre><code>Parent p = new Parent(); Parent p1 = p; Parent p2 = p; Parent p3 = p2; </code></pre> <p>Here, there are four references (<code>p</code>, <code>p1</code>, <code>p2</code>, and <code>p3</code>) each of which points to the same object you created with the <code>new Parent()</code> declaration.</p> <p>I would probably argue on the philosophical point, though, that this creation of new references is actually explicit rather than behind the scenes when you say <code>Parent p = something</code>.</p> <p><strong>Links:</strong></p> <ul> <li><a href="http://en.wikipedia.org/wiki/Primitive_wrapper_class" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Primitive_wrapper_class</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html</a></li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/ClassCastException.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/7/docs/api/java/lang/ClassCastException.html</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/java/IandI/override.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/tutorial/java/IandI/override.html</a></li> <li><a href="https://stackoverflow.com/questions/40480/is-java-pass-by-reference">Is Java &quot;pass-by-reference&quot; or &quot;pass-by-value&quot;?</a></li> </ul>
    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.
    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