Using raw types for generics, the bounds get ignored, too. However only the bounds of the highest level class. The parent class keeps its bounds. This seems like inconsistent behaviour.
public class GenericType
{
public static void main(String[] args)
{
ExampleExtended<?> example1 = null;
ExampleExtended example2 = null;
Integer result1 = example1.get();
Number result2 = example2.get();
}
}
abstract class GenericExample<C extends Number>
{
public abstract C get();
}
abstract class ExampleExtended<C extends Integer> extends GenericExample<C>
{
}
The expected return type of example2.get() is Integer, due to the defined bounds of ExampleExtended, as it is the case for "example1" with a wildcard. However, it seems due to the raw type, for some reason, the bounds are ignored and the return type is NOT Integer.
Now you would expect the return type to be "Object". Since the bounds of ExampleExtended are ignored it would be consistent if they are ignored for GenericExample as well, after all. This is NOT the case, either! The return type is Number!
Why? This seems so oddly inconsistent to me.