How to cast parent into child in Java

46,719

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.

Share:
46,719

Related videos on Youtube

likaa
Author by

likaa

Updated on July 09, 2022

Comments

  • likaa
    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 from Parent class, it contains the Parent 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
      Oliver Charlesworth over 6 years
      It is possible to do it like that, iff parent actually refers to a Child instance.
    • Mureinik
      Mureinik over 6 years
      What type is parent?
    • likaa
      likaa over 6 years
      Well 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
      Jesper over 6 years
      What 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 do new Child() instead of new Parent().
    • likaa
      likaa over 6 years
      I 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
      Mick Mnemonic over 6 years
      If 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 to Child. 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
      likaa over 6 years
      I 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
    likaa over 6 years
    I 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
    likaa over 6 years
    Hmm, this is possible, but still need to set parameters line by line.
  • Stephan-MDD
    Stephan-MDD over 6 years
    what is the int fields for ? @likaa
  • likaa
    likaa over 6 years
    I 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
    Akash Roy Choudhury over 6 years
    well you could also move from inheritence to composition model.
  • likaa
    likaa over 6 years
    @AkashRoyChoudhury That is very good idea, if I wont find better solution i'll use it!
  • Akash Roy Choudhury
    Akash Roy Choudhury over 6 years
    @likaa ok so you just want an easy way to set the properties. See updated answer
  • fuat
    fuat about 4 years
    what if parent class fields are private ?