Liutauras Vilda wrote:@OP
List<? super Integer> foo = new ArrayList<Integer>(); // Integer is a "superclass" of Integer (in this context)
You can add an Integer because an Integer is allowed in any of the above lists
Anil Philip wrote:I have been struggling to understand this concept. Isn't the code snippet below, similar to what you wrote above?
Yet it does not compile
Anil Philip wrote:Isn't the code snippet below, similar to what you wrote above?
Liutauras Vilda wrote:You can't add an A because foo could be pointing to an ArrayList<Object>
Anil Philip wrote:But the compiler knows that we are only adding an A to a List<A> yet it disallowed it!
Does List<? super B> mean every member should be the same (all As, all Bs, all Objects)
Is my understanding correct?
Stephan van Hulst wrote:
It's not helping that you've chosen type names that are meaningless. Let's use different examples:
Liutauras Vilda wrote:@OP All can get confusing, but when you think about it slowly, all make sense.
Anil Philip wrote:
In other words, it has defeated the original restriction.
Liutauras Vilda wrote:We have explained that thrice.
Liutauras Vilda wrote:Ah no no, I'm not annoyed, apologies if I sounded that way
Let's get back to business
You know, when you say new ArrayList<C>(), you say that this array list would hold C's. But that's not true, right? Because on the left side you say List<? super B>.
However, when you have an empty diamond operator <>, it infers the type this array list could hold based on the declaration on the left hand side, then it compiles just fine, and I trust you understand why to add C is allowed.
Anil Philip wrote:...add the C's one by one, or bulk copy, we end up with the result that was denied earlier - an ArrayList of Cs.
Liutauras Vilda wrote:
Anil Philip wrote:...add the C's one by one, or bulk copy, we end up with the result that was denied earlier - an ArrayList of Cs.
I know I know what you mean. But that's not accurate.
Consider this:
Now if you say new ArrayList<C>(), that contradicts what List<? super B> says:
"You can add a B or an instance of a subclass of B"
F is indeed a subclass of B, same as C is a subclass of B.
Is it clear now?
There are three kinds of actuaries: those who can count, and those who can't.
Anil Philip wrote:
Am I understanding you correctly?
Anil Philip wrote:If I understand what you are saying,
we should be able to have both F's and C's in the ArrayList since both are subclasses of B.
The only way to have this is to deny or disallow
new ArrayList<C>()
Am I understanding you correctly?
Liutauras Vilda wrote:
Anil Philip wrote:If I understand what you are saying,
we should be able to have both F's and C's in the ArrayList since both are subclasses of B.
The only way to have this is to deny or disallow
new ArrayList<C>()
Am I understanding you correctly?
Well, yes, otherwise contract isn't respected.
Apologies didn't reply earlier - missed your message.
Create symphonies in seed and soil. For this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|