Jyothsna Panchagnula wrote:Will this code complies successfully or not ?
To my knowledge the answer is 'Yes' but when I executed it using eclipse editor it gives compile time exception.
Why 'Yes' according to me is as follows:
?--> denotes any type
extends -->denotes either implements or extends the type on the right hand side which means that a sub class that extends/implements Number is allowed.
super --> denotes that the type on the right hand side of super and its parent classes are allowed.
since Integer is a subclass of Number , this code should work , but it throws compile time exception
Can any one let me know where am I going wrong here?
To my knowledge the answer is 'Yes' but when I executed it using eclipse editor it gives compile time exception.
Why 'Yes' according to me is as follows:
?--> denotes any type
extends -->denotes either implements or extends the type on the right hand side which means that a sub class that extends/implements Number is allowed.
super --> denotes that the type on the right hand side of super and its parent classes are allowed.
since Integer is a subclass of Number , this code should work , but it throws compile time exception
Can any one let me know where am I going wrong here?
Maduranga Liyanage wrote:I\m pretty confused with this.
From the above example, I should NOT be able to compile this:
But this compiles. For all we know, q1 can be Number, Integer or Byte and q2 can be Integer or a class thereof.
Therefore, how can the compiler allow me to do this but not this:
Maduranga Liyanage wrote:Thank you.
But doesnt q1 (<? extends Number>) has the possibility of being of type Byte or any of the other wrapper classes?
And q2 can be Integer or thereof. So assigning q1=q2 is OK?
I'm sorry if I mistaking something gravely here.
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|