Create instance from superclass instance
<p>Consider the following case:</p>

<pre><code>class A {
  int x;
  int y;
}

class B extends A {
  int z;
}
</code></pre>

<p>Now, somewhere in the code this classes are used like this:</p>

<pre><code>A objA = getAFromSomewhere();
B objB = null;
</code></pre>

<p>And in a certain situation I want to do something like</p>

<pre><code>objB = objA; // can't do this
objB.z = someZ;
</code></pre>

<p>Of course the real objects are a bit more complicated, so it's not just about copying two ints. But they aren't overly complex either.</p>

<p>I know I can write a constructor for B like this:</p>

<pre><code>public B(A anA) {
  this.a = anA.a;
  this.b = anA.b;

  this.z = 0;
}
</code></pre>

<p>But if that's really the only way, I prefer merging the additional members of B into A.</p>

<p><strong>update considering the answers</strong></p>

<p>My question was not clear enough. I understand that objB = objA; can't work (thus I asked for "something like", meaning something with comparable code complexity) and I know about the issues with shallow vs deep copies.<br>
What I was looking for is a possibility to copy the members of a base class (let's say using clone()). You may understand that copying every member manually is a bad solution as it adds complexity and redundancy to the code. Thanks for your replies anyway!</p>
