Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create instance from superclass instance

Tags:

Consider the following case:

class A {   int x;   int y; }  class B extends A {   int z; } 

Now, somewhere in the code this classes are used like this:

A objA = getAFromSomewhere(); B objB = null; 

And in a certain situation I want to do something like

objB = objA; // can't do this objB.z = someZ; 

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.

I know I can write a constructor for B like this:

public B(A anA) {   this.a = anA.a;   this.b = anA.b;    this.z = 0; } 

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

update considering the answers

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.
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!

like image 683
didi_X8 Avatar asked Aug 03 '11 10:08

didi_X8


People also ask

Can we create a object of superclass using subclass?

No. It makes zero sense to allow that. The reason is because subclasses generally define additional behavior. If you could assign a superclass object to a subclass reference, you would run into problems at runtime when you try to access class members that don't actually exist.

Can you create an instance of a class?

When you create an object, you are creating an instance of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object.

Can a superclass be an instance of a subclass?

It is possible to reference a subclass as an instance of one of its superclasses. For instance, using the class definitions from the example in the previous section it is possible to reference an instance of the Car class as an instance of the Vehicle class.


2 Answers

There's no trivial solution to this because there's no one-size-fits-all solution. Basically you don't have all the information within a B, so you can't guarantee you would have a "sensible" B object.

You probably just want to create a constructor in B which takes an A and copies all the A data into the new B.

like image 117
Jon Skeet Avatar answered Oct 14 '22 12:10

Jon Skeet


If you're not scared of commons-beanutils you can use PropertyUtils

import org.apache.commons.beanutils.PropertyUtils; class B extends A { B(final A a) { try {         PropertyUtils.copyProperties(this, a);     }     catch (Exception e) {     } } } 
like image 32
JTP Avatar answered Oct 14 '22 12:10

JTP