Class X{
Class<B> object;
public setObject( Class<B> a ){....}
}
Interface B{}
Interface C extends B {}
I instantiate X like this,
X x = new X();
x.setObject( C.class );
When i build the code it complains required Class<B>
found Class<C>
. Since C extends B, can i not use C.class? If so can some one explain why?
I i do the same thing using Spring`s XML based bean creating it works just fine. The bean definition would be
<bean id="dummy" class="X">
<property name"object" value="C">
</bean>
This works just fine. Im not getting why instantiating in java would fail.
this is not valid Java code
It works because there is type erasure done for parameters <>
the java bytecode looks exactly the same as the one for
class X{
Class object;
public void setObject( Class a ){....}
}
interface B{}
interface C extends B {}
It looses the type checks information, it is only used at compile time.
You can use this to make it work in the code:
class X{
Class<? extends B> object;
public void setObject( Class<? extends B> a ){....}
}
interface B{}
interface C extends B {}