title: Does upcasting ensure the preservation of a child's field in java?
body: <p>I'm going to use Hibernate for a persist some entities.</p>
  
  <pre><code>EntityClass entity = new EntityClass();
  Object obj = (Object) entity;
  hibernateSession.persist(obj)
  </code></pre>
  
  <p>Can I ensure that the field value in the original EntityClass will be preserved?</p>
  
  <p>Does the garbage collector not clear field values?</p>
  
  <p>In all other cases, can I ensure that the upcasting preserves the value of the original field?</p>
  
  <p>(other common cases that don't use hibernate)</p>
  
  <p>It is my original code</p>
  
  <pre><code>Class&lt;?&gt; clazz = Class.forName(~~~); // from external file
  Iterator&lt;?&gt; iterator = ((Iterator&lt;?&gt;) clazz.getConstructor(String.class).newInstance(~~~); // from external file
  
  while (iterator.hasNext()) {
      for (JsonEntity content : (List&lt;JsonEntity&gt;) iterator.next()) {
          session.saveOrUpdate(content.toEntity());
      }
      session.flush();
  }
  transaction.commit();
  
  </code></pre>