Reputation: 303
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:
:
class Parent{
public int parameter1;//...
public int parameter1000;
}
class Child extends Parent
{
public Child(Parent parent)
{
this.parameter1 = parent.parameter1;//...
this.parameter1000 = parent.parameter1000;
}
}
Upvotes: 27
Views: 85129
Reputation: 47
Do you really want to cast you parent into the child class? If the given Object (given as a Parent object) was created as a child class, you can access the correct functions without the cast. Here is an example: In the main you see a Vector 'ps' to store Parent objects, but added are Child objects which extend from the Parent. While iterateing through that vector, I dont know which kind of child I get next (thus I cannot cast to the correct class), but due to the keyword Override of the function I call, the correct child/method is used.
import java.util.Vector;
import lib.Parent;
import lib.C1;
import lib.C2;
public class MyClass {
public static void main(String []args){
System.out.println("Hello World");
C1 c1 = new C1();
C2 c2 = new C2();
ps.add(c1);
ps.add(c2);
for(int k = 0; k < ps.size(); k++){
ps.get(k).setInteger(); // no cast needed
System.out.println("stored entity " + ps.get(k).i);
}
// or use a ranged for loop
for(Parent p: ps){
p.setInteger();
System.out.println("stored entity " + p.i);
}
}
static Vector<Parent> ps = new Vector<>();
}
The Parent class: The keyword abstract forces every child to implement that method, so if you have a Parent object you can safely call this method. Since the Parent class itself is abstract you wont have an object which is just a Parent object since that is not allowed and wont compile.
package lib;
public abstract class Parent{
public Integer i;
public abstract void setInteger();
}
And here are two Child classes which have an different implementation of the setInteger() method.
package lib;
import lib.Parent;
public class C1 extends Parent{
@Override
public void setInteger(){
i = new Integer(1);
}
}
Second Child:
package lib;
import lib.Parent;
public class C2 extends Parent{
@Override
public void setInteger(){
i = new Integer(2);
}
}
The Output is
stored entity 1
stored entity 2
stored entity 1 <-- ranged for
stored entity 2 <-- ranged for
Edit: The file structre looks like this
|-MyClass.java |-lib |-Parent.java |-C1.java |-C2.java
Upvotes: 0
Reputation: 361
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.
Upvotes: 33
Reputation: 326
change 'this' to 'super' in your child constructor, and remove the parent parameter, instead replace that with the two parameters int parameter1;//...int parameter1000; :)
class Parent{
public int parameter1;//...
public int parameter1000;
}
class Child extends Parent
{
public Child(int parameter1, int parameter1000)
{
super.parameter1 = parameter1
super.parameter1000 = parameter1000;
}
}
Upvotes: 0