How do you cast a List of supertypes to a List of subtypes?

前端 未结 17 1289
面向向阳花
面向向阳花 2020-11-22 08:43

For example, lets say you have two classes:

public class TestA {}
public class TestB extends TestA{}

I have a method that returns a L

17条回答
  •  情深已故
    2020-11-22 09:11

    I think you are casting in the wrong direction though... if the method returns a list of TestA objects, then it really isn't safe to cast them to TestB.

    Basically you are asking the compiler to let you perform TestB operations on a type TestA that does not support them.

提交回复
热议问题