I have a parent and child class defined below:
public class Parent {
public int values_ = 0;
public void setValue(int v)
{
this.values_ = v;
}
}
and a Child class as below
public class Child extends Parent {
public double key = 3;
}
I would like have a list of children in which each of the child in the children list will have the propertie of each of the parent object in the parent list.
I tried something like this:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Parent> parentList = new ArrayList<Parent>();
Parent p1 = new Parent();
p1.setValue(10);
parentList.add(p1);
Parent p2 = new Parent();
p1.setValue(20);
parentList.add(p2);
ArrayList<Child> childrenList = new ArrayList<Child>();
for(Parent p : parentList)
{
Child c = new Child();
System.out.println(c.values_);
System.out.println(c.key);
childrenList.add(c);
}
}
}
But it does not work. My children still have the default values_ from the parent and not the values set
How can I go about this?