2

I have two ArrayLists that contain Parent and Child class which Child extents Parent and Second extends First

public First(ArrayList<Parent> parents)
{
    // Parent Class's constructor
}

second class's constructor

public Second(ArrayList<Child> child)
{
    super(child);
    // child class's constructor take ArrayList<Child>
}

Is it possible to cast ArrayList<Child> to ArrayList<Parent>?

4
  • You'll have to loop over every value of the list and cast it. Commented Oct 7, 2013 at 3:37
  • 2
    How about ArrayList<? extends Parent>? Commented Oct 7, 2013 at 3:42
  • Or you could declare your method parameter as ArrayList<? extends Parent>, perhaps? It's not really clear what you want to do. Do you want to pass an ArrayList of Child to First? Commented Oct 7, 2013 at 3:42
  • Init a Second which is able to take ArrayList<Child>. As Parent and Child, First and Second are quite similar, so I am looking for a way to subclass them. Commented Oct 7, 2013 at 3:56

4 Answers 4

8

This is the way to cast from Parent to child

public static void main(String[] args) {
    ArrayList<Foo> parentArray = new ArrayList<Foo>();
    parentArray.add(new Foo("tooLazy"));
    parentArray.add(new Foo("tooAdd"));
    parentArray.add(new Foo("tooMoreFields"));
    ArrayList<Boo> childArray =  (ArrayList<Boo>) ((ArrayList<?>) parentArray);
}

And this is the way to cast from Child to parent

//Boo extends Foo BTW
public static void main(String[] args) {
    ArrayList<Boo> parentArray = new ArrayList<Boo>();
    parentArray.add(new Boo("tooLazy"));
    parentArray.add(new Boo("tooAdd"));
    parentArray.add(new Boo("tooMoreFields"));
    ArrayList<Foo> childArray =  (ArrayList<Foo>) ((ArrayList<?>) parentArray);
}
Sign up to request clarification or add additional context in comments.

8 Comments

There will be a unchecked warning.
Yeah, but what is the big deal if you already know that those classes are parent and child?. Sure, there are better ways like ArrayList<? extends Parent>.
Ya you are right, both ArrayList<? extends Parent> and (ArrayList<Foo>) ((ArrayList<?>) parentArray) are good. Thanks
The big deal is that it is not type safe. In the class First I could add to the list parents an instance of BlackSheep if BlackSheep extends Parent. The compiler won't stop me from doing that.
This humble monkey coder kneels on your wisdom almighty @JudgeMental (Really, your explanation is right. Doing like I do is the Billy way)
|
4

Credit goes for Churro.

@SuppressWarnings("unchecked")
public Second(ArrayList<? extends Parent> child)
{
    super((ArrayList<Parent>) child);
}

1 Comment

How does this solve your problem? It seems from the question that you really need the element type of your list to be Child and you are losing that here.
1

use this.addAll(child) in the constructor of second.

Its not casting but it will create a new Arraylist with the contents of the child

Comments

0

The only way this relationship is kosher is if, in the constructor of Parent, you are not modifying the input list in any way except perhaps to clear it or add nulls to it. In that case you might as well make the argument type of the constructor of First be ArrayList<? extends Parent>. Then there is no need to copy the array or do unsafe casts.

I should add that unless you are relying on specific properties of the ArrayList class, List<? extends Parent> would be even better.

2 Comments

But this is sacrifice the type check in First?
How so? First will only accept a list whose elements are all of some class Parent or more specific than Parent. That allows a list of Child to be passed in too. According to your question that is your intention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.