user3841581
user3841581

Reputation: 2747

How to get list of children objects from a list of parent objects in java

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?

Upvotes: 3

Views: 4654

Answers (3)

Ghanshyam
Ghanshyam

Reputation: 182

Try this code:

class Parent {
    public int values_ = 0;

    public Parent(int values) {
        this.values_= values; 
    }
}
class Child extends Parent {
    public double key = 3;

    public Child(int values) {
        super(values); 
    }

    @Override 
    public String toString() {
        return "[" + key + " " + values_ + "]";
    }
}
public class TestInherit {

    public static void main(String...string) {
        ArrayList<Parent> parentList = new ArrayList<Parent>();

        Parent p1 = new Parent(10);
        parentList.add(p1);

        Parent p2 = new Parent(20);
        parentList.add(p2);

        ArrayList<Child> childrenList =  new ArrayList<Child>();
        for(Parent p : parentList)
        {
            Child c = new Child(p.values_);
            childrenList.add(c);
        }
        System.out.println(childrenList);
    }

Upvotes: 2

Anshul Sharma
Anshul Sharma

Reputation: 1123

What you are doing here is not right, you are simply iterating through parent-list and creating a new child in each iteration but not assigning child object any value, so it will have default value.

try this :

public class Parent {
  public int values_ = 0;

  public void setValue(int v)
  {
    this.values_ = v;
  }
}
public class Child extends Parent {
   public double key = 3;
}
public static void main(String[] args) {
    List<Parent> parentList = new ArrayList<Parent>();

    Parent p1 = new Parent();
    p1.setValue(10);
    parentList.add(p1);

    Parent p2 = new Parent();
    p2.setValue(20);
    parentList.add(p2);


    List<Child> childrenList =  new ArrayList<Child>();
    for(Parent p : parentList) {
        Child c =  new Child();
        c.values_ = p.values_;
        childrenList.add(c);
    }

    System.out.println(childrenList);
}

Upvotes: 2

WJS
WJS

Reputation: 40034

How about something like this. I added constructors of the classes.

class Parent {
    public int values_ = 0;

    public Parent(int values) {
        this.values_= values;
    }
    public void setValue(int v) {
        this.values_ = v;
    }

}

class Child extends Parent {
    public double key = 3;

    public Child(int values) {
       super(values);
    }
    public Child(double key, int values) {
        super(values);
        this.key = key;
    }
    @Override 
    public String toString() {
        return "[" + key + " " + values_ + "]";
}

ArrayList<Parent> parentList = new ArrayList<Parent>();

Parent p1 = new Parent(10);
parentList.add(p1);

Parent p2 = new Parent(20);
parentList.add(p2);

ArrayList<Child> childrenList = new ArrayList<Child>();
int key = 4;
for (Parent p : parentList) {
     // add values from parent and key from here
    Child c = new Child(key++, p.values_);
    // if wanted to use the default key in the child class,
    // then just use the value constructor
    //  Child c = new Child(p.values_);
    childrenList.add(c);
}

System.out.println(childrenList);


Prints

[[4.0 10], [5.0 20]]

Upvotes: 2

Related Questions