Consider the following scenario involving two classes:
public class TestA {} public class TestB extends TestA {}
You have a method that returns a List
Casting to List
List<TestB> variable = (List<TestB>)(List<?>) collectionOfListA;
This approach is allowed because you can cast to and from wildcard types, albeit with an unchecked warning. The wildcard type acts as an intermediate step, allowing you to cast the list of supertype elements to a list of subtype elements.
The above is the detailed content of How Can I Safely Cast a List of Supertypes to a List of Subtypes in Java?. For more information, please follow other related articles on the PHP Chinese website!