java - Convert Arraylist<subclass> to ArrayList<parent> -
i have 2 arraylists contain parent
, child
class child
extents parent
, 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 possible cast arraylist<child>
arraylist<parent>
?
this way cast parent 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 way cast child 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); }
Comments
Post a Comment