Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast parent into child in Java

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;
}
}
like image 322
likaa Avatar asked Dec 02 '17 18:12

likaa


People also ask

Can I cast a parent to child in Java?

However, a parent may or may not inherits the child's properties. However, we can forcefully cast a parent to a child which is known as downcasting. After we define this type of casting explicitly, the compiler checks in the background if this type of casting is possible or not.

Can we assign parent object to child objects in Java?

In real world, parents can accommodate children but children cannot accommodate parents. same is the case in OOP. class Parent { int prop1; int prop2; } class Child : Parent // class Child extends Parent (in case of Java Lang.)

How do you inherit a parent class method to a child class in Java?

Use Java's extends keyword to derive a child class from a parent class, invoke parent class constructors and methods, override methods, and more. Java supports class reuse through inheritance and composition.

How do you cast a superclass to a subclass in Java?

You can try to convert the super class variable to the sub class type by simply using the cast operator. But, first of all you need to create the super class reference using the sub class object and then, convert this (super) reference type to sub class type using the cast operator.


1 Answers

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.

like image 50
Akash Roy Choudhury Avatar answered Oct 24 '22 09:10

Akash Roy Choudhury