Change object type at runtime
<p>I have the following scenario:</p>

<pre><code>class Parent {}

class Child1 extends Parent {}

class Child2 extends Parent {}

interface ExternalSystem {
    Parent materialize(Parent a)
}

class Child1System implements ExternalSystem {
    Parent materialize(Parent a) {
        // specialize a and convert it into Child1
    }
}

class Child2System implements ExternalSystem {
    Parent materialize(Parent a) {
       //  specialize a and convert it into Child2
    }
}
</code></pre>

<p>I want to let users create objects of type <code>Parent</code>. Some components of the system are OK with the information that <code>Parent</code> provides. At some point though, I want to materialize these objects in an external system and depending on which <code>ExternalSystem</code> implementation I call, <code>Parent</code> objects should now be specialized objects.</p>

<p>Is there a way of creating a new child object and replace the space used by the parent object?</p>

<p>I can't just replace <code>Parent a</code> object with a new object of type <code>Child1</code> or <code>Child1</code> because <code>a</code> could have been referenced in other places. So, essentially, what I want is all references to keep looking at <code>Parent a</code> but instead of <code>a</code> being of type <code>Parent</code> it will now be of a child type.</p>

<p>Alternatively, can this be designed differently?</p>
