I have files Card.java and Util.java which I am not allowed to modify. For my own purposes, I created a class Card2 which extends Card, and adds an equals()
method and a hashcode()
method. The Util class contains a method which takes an ArrayList<Card>
as a parameter. I have an ArrayList<Card2>
and I want to run that method.
My understanding is that, in this case, Card2
has an "is-a" relationship with Card
, so I would think there should be a way to convert the ArrayList<Card2>
to ArrayList<Card>
.
I'm unsure how to go about doing this conversion though. I tried (ArrayList<Card>) arr
where arr
is an ArrayList<Card2>
, but I get an error stating these are inconvertible types. I realize I could create a new ArrayList<Card>
and add each of the cards in arr
independently, but I'm wondering whether there's a better way to go about this.