Java = What are alternatives / solutions to downcasting?
<p>I have Value-Objects/Beans (only containing members, no logic):</p>

<pre><code>public class Parent {
String first;
String second;
}
</code></pre>

<p>Some processing logic returns the "Parent". I then do some further processing and want to add furtehr fields:</p>

<pre><code>public class ParentAddedMembers extends Parent {
String third;
String fourth;
} 
</code></pre>

<p>The Problem is, I can NOT downcast from <code>Parent</code> to <code>ParentAddedMembers</code>.</p>

<pre><code>ParentAddedMembers parentAddedMembers = (ParentAddedMembers) parent;
</code></pre>

<p>This seems to be invalid.</p>

<p>(From my point of view in this case it would be legal, when downcasting the unassigned, new fields would simply hold nulls. But it seems java does not allow this).</p>

<p>What is the correct solution, if I do not want to copy all fields manually (I could write a copy method that copies the Parent members to a newly created ParentAddedMembers. But this does not work for private fields. Furthermore it will break very easyly if I add/delete membes in parent...)</p>

<p>What is the corret solution for this?</p>

<p>Thanks very much!</p>

<p>Markus</p>

<p><strong>Update:</strong>
What I want to achive is. I have a thirdparty Library that returns some Objects Parent (from a search result), but I need to add further fields (metadata) to it. Downcasting, as described would solve the problem easily but does not work. I also can not change the parent as it is from a third party lib.</p>
