How to cast parent into child in Java
Well you could just do :
Parent p = new Child();
// do whatever
Child c = (Child)p;
Or if you have to start with a pure Parent object you could consider having a constructor in your parent class and calling :
class Child{
public Child(Parent p){
super(p);
}
}
class Parent{
public Parent(Args...){
//set params
}
}
Or the composition model :
class Child {
Parent p;
int param1;
int param2;
}
You can directly set the parent in that case.
You can also use Apache Commons BeanUtils to do this. Using its BeanUtils class you have access to a lot of utility methods for populating JavaBeans properties via reflection.
To copy all the common/inherited properties from a parent object to a child class object you can use its static copyProperties() method as:
BeanUtils.copyProperties(parentObj,childObject);
Note however that this is a heavy operation.
Related videos on Youtube
likaa
Updated on July 09, 2022Comments
-
likaa almost 2 years
I am doing this:
Child child = (Child)parent;
Which gives me an error, I found it isn't possible to do it like this. I don't know exactly why, but I think it should be possible, if
Child
class inherits fromParent
class, it contains theParent
object data.My questions are:
- Why it doesnt work?
- How can i make this work, without setting every single parent's attribute like this
:
class Parent{ public int parameter1;//... public int parameter1000; } class Child extends Parent { public Child(Parent parent) { this.parameter1 = parent.parameter1;//... this.parameter1000 = parent.parameter1000; } }
-
Oliver Charlesworth over 6 yearsIt is possible to do it like that, iff
parent
actually refers to aChild
instance. -
Mureinik over 6 yearsWhat type is
parent
? -
likaa over 6 yearsWell i have some data loaded from file, something like: public Parent loadMe() { Parent p = new Parent(); ... return p; } and then i need to recast this parent to some of its children
-
Jesper over 6 yearsWhat you are trying to do is not possible. A dog is an animal, but an animal is not (always) a dog. Casting does not magically convert one type of object into another. It just tells the compiler that you know better than the compiler what the actual type of the object is. If you need a
Child
, then you need to donew Child()
instead ofnew Parent()
. -
likaa over 6 yearsI have multiple children, I cant just make 20 methods with the same content, only returning the one child, it is possible, but I believe it could be done much more easier and better.
-
Mick Mnemonic over 6 yearsIf you shared some more code, it might become clearer what you're actually trying to do. If your data-loading code is only creating instances of
Parent
, it's not possible to cast these objects toChild
. Do you even need inheritance here? Could you just have a field called "type" on the parent class to determine what kind of object (child) is in question? -
likaa over 6 yearsI have data structures very similar to each other, the same attributes i have in parent class and differents in children classes, that is why i am using inheritance, because of the same attributes (like 90% is same).
-
likaa over 6 yearsI guess i dont understand your answer, i wanted to show that i have multiple parameters in parent class (that is why parameter1 ... parameter1000) and my question was, how to do it without 1000 lines of setting this parameters.
-
likaa over 6 yearsHmm, this is possible, but still need to set parameters line by line.
-
Stephan-MDD over 6 yearswhat is the int fields for ? @likaa
-
likaa over 6 yearsI dont really have int fields in the parent class, i have different attributes, and some of them are different types, if i have only int params, i would put it into array.
-
Akash Roy Choudhury over 6 yearswell you could also move from inheritence to composition model.
-
likaa over 6 years@AkashRoyChoudhury That is very good idea, if I wont find better solution i'll use it!
-
Akash Roy Choudhury over 6 years@likaa ok so you just want an easy way to set the properties. See updated answer
-
fuat about 4 yearswhat if parent class fields are private ?