Consider the following code, where Feline
and Animal
are interfaces and Feline extends Animal
.
List<Feline> miaus = foo.retrieveMiaus();
List<Animal> animals = xxxx;
bar.pet(animals);
What is the best way to cast miaus to animals - i.e the xxxx
?
One way is to:
List<Animal> animals = (List<Animal>) (List<?>)miaus;
But it has unchecked casts - and it downright looks ugly.
One can also iterate over miaus
and populate the animals by casting each Feline
to Animal
, but this isn't ideal either.
Is there any cleaner way to do this?