Forums Register Login

Why not compile?

+Pie Number of slices to send: Send
 

import java.util.*;
class test {
public static void main(String[] args) {
List<? super Integer> list = new ArrayList <Object> ();
list.add(new Object());
list.add(23);
list.add(23.0);
}
}
Why not compile?

 
+Pie Number of slices to send: Send
I think its because of list.add(23.0); as it will be wrapped up into a Float object which is not a sub-class of Integer....

Please correct me as I am very weak at Generics...
+Pie Number of slices to send: Send
 

Originally posted by Ankit Garg:
I think its because of list.add(23.0); as it will be wrapped up into a Float object which is not a sub-class of Integer....

Please correct me as I am very weak at Generics...



correct

then wat aboout list.add(new Object())?

Hope This Helps
+Pie Number of slices to send: Send
By using "List<? super Integer>" in our type declaration of list, we include only elements who are either of type Integer or elements for whom Integer is super class.
+Pie Number of slices to send: Send
 

Originally posted by seetharaman venkatasamy:


then wat aboout list.add(new Object())?



I also thought that but I didn't say it as I didn't wanted to sound really stupid if I was wrong....
+Pie Number of slices to send: Send
Hi Varinder,

The code will not compile as it is declared
<code>
List<? super Integer> list
</code>

when calling list.add(new Object())
so compiler is unsure about which super class of Integer will be added.
So compiler restricts you from adding. However you can add an Integer only.
<code>
List<? super MyClass> list
</code>

you can add object of MyClass or any of its subclass
Hope this ll give you a little better idea.

Thanks,
Manoj Mishra
SCJP1.5 97% on yesterday
+Pie Number of slices to send: Send
 

Originally posted by varinder mahajan:
import java.util.*;
class test {
public static void main(String[] args) {
List<? super Integer> list = new ArrayList <Object> ();
list.add(new Object());
list.add(23);
list.add(23.0);
}
}
Why not compile?
[QB][/QB]



Hi!

List<? super Integer> list declares a List of an (almost) unknown type. The only thing you know about that unknown type is that its an Integer or a supertype of an Integer.
Once the List is created at runtime , it will, like all Lists, accept elements of that (almost unknown at compile time)type and its SUBTYPES.
How can the compiler know for sure this will happen ?
By not allowing you insert elements that are supertypes of Integer.
That way, no matter the type of List you create at runtime(Integer or supertype of it) its guarentted they are all subtypes of the type of List created.

Hope it helps

Fernando
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 822 times.
Similar Threads
Generic methods and wildcards
A Generic Question
Generics doubt
Generic Question in JAVA Beat
GENERICS
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:55:49.