Under what circumstance is it safe to cast an object from a List<? extends Fruit>?

Disable ads (and more) with a premium pass for a one time $4.99 payment

Dive into the depths of Java with our quiz based on "Thinking in Java, Fourth Edition" by Bruce Eckel. Test your knowledge, solidify concepts, and prepare for certification with challenging questions and insightful feedback.

When casting an object from a List<? extends Fruit>, the only safe circumstance is when you are completely certain of the list's element types. This means that you know, without a doubt, that all elements in the list are of a specific subtype of the Fruit class. Option A is incorrect because simply casting to Fruit or any of its subclasses is not always safe. Option B is incorrect because relying on the fact that the list can only contain Apples is not a guarantee of safety. Option D is incorrect because there are some situations where it is safe to cast from such a list, as in the case of being certain of the element types.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy