I do not understand why the compiler can not see that a cast is safe when the parameterized type is defined as extending a base class. Here are examples of casts that seem to me like they should be unnecessary. Further, when I do include the cast, my IDE (IntelliJ IDEA) warns that the cast is unchecked, as if to suggest that I am doing something wrong. Is there an idiom that avoids these casts and warnings? Why are the casts needed at all, given that the declaration states that the type extends the base class?
class Shape {}
class Polygon extends Shape {}
public class Foo<T extends Shape>
{
Set<Polygon> polygons;
// Why must this be cast?
Set<T> shapes = (Set<T>) new HashSet<Polygon>();
T getFirst()
{
// Why must this be cast?
return (T) polygons.iterator().next();
}
Iterable<T> getShapes()
{
// Why must this be cast?
return (Iterable<T>) polygons;
}
}
Let’s assume you’ve instantiated your class like this:
Then,
Set<Circle>cannot be safely assignedHashSet<Polygon>In
getFirst: You cannot safely castPolygontoCircleAnd in
getShapes: you cannot safely castIterable<Polygon>toIterable<Circle>.