Mastering Java: The Ultimate 2025 All-In-One Quiz to Excel in 'Thinking in Java

Question: 1 / 510

Why can't you upcast a List<Apple> to a List<Fruit>?

Because the List types are invariant

Because a Fruit is not an Apple

Because of runtime type information

Because generics do not support covariance by default

Upcasting is the process of converting an object of a subtype to its supertype. It is usually allowed when working with inheritance, but it is not possible with generic types by default. This is because generics are invariant in Java, meaning that subtypes cannot be used in place of their supertype. So, even though an Apple is a subtype of Fruit, a List<Apple> cannot be used as a List<Fruit>. This is because it would violate type safety as the List<Fruit> could end up containing other types of Fruit that are not Apples. Therefore, options A, B, and C are incorrect because they do not address the specific reason why upcasting a List<Apple> to a List<Fruit> is not allowed in Java.

Get further explanation with Examzify DeepDiveBeta
Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy