Compiler errors with bounded wildcard
Hi.
Given the following classes A and generic B:
class A
{
publicvoid method(Class<?extends Number> c){}
}
class B<T>
{
publicvoid method(Class<?extends Number> c){}
}
The following lines produce compiler errors, as expected:
A a =new A();
a.method(Integer.class);
a.method(String.class);// compiler error, since String is not a subclass of Number
B<Boolean> b1 =new B<Boolean>();// Boolean doesn't matter here
b1.method(Integer.class);
b1.method(String.class);// compiler error, since String is not a subclass of Number
B<?> b2 =new B();// B with a wildcard
b2.method(Integer.class);
b2.method(String.class);// compiler error, since String is not a subclass of Number
But, why the following does compile?
B b3 =new B();// B with no specific type
b3.method(Integer.class);
b3.method(String.class);// compiles, but why?
I don't understand why the last example compiles, since the bounded wildcard of method has nothing to do with the generic type T of class B.
So I don't see why using a B without specific type T allows to call method with a class that does not fulfill the bounded willdcard.
God bless
Jaime
[2153 byte] By [
jsaiza] at [2007-11-27 8:31:07]

# 3
> A<rawtype> is NOT A<?>.
Thanks, I see it.
But the question was why the bounded wildcard in a method that has nothing to do with the parametrized type of B was ignored.
In fact, A is a plain type and the bounded wildcard is checked.
The response from dannyyates answers the question.
Well, I just didn't see this explanation in any tutorial of Generics.
Thank you.
jsaiza at 2007-7-12 20:26:28 >

# 5
> It's mentioned in section 6.1 of Sun's generics tutorial
From the generics tutorial, section 6.1, paragraph "Using Legacy Code in Generic Code":
"if generic code is going to call legacy code, this has to be allowed"
"raw types are very much like wildcard types, but they are not typechecked as stringently"
Are the most suitable phrases (for me) to explain this issue.
However, from the example provided there, I could understand that operations with type T would not be checked for a raw B object, but I don't see an example similar to the one I provided, in which a generic method that has nothing to do with the parametrized type is not checked, when the same method in a plain object (like class A above) is checked.
Anyway... I just have to write B<?> instead of B (a bit more to write) if I want to have this kind of checks.
Thank you again!
jsaiza at 2007-7-12 20:26:28 >
