Forums Register Login

Generics

+Pie Number of slices to send: Send
Hi, Please advice what is the difference between the following 2 statements..
+Pie Number of slices to send: Send

Please tell us what you think these two statement means, and how you think they are different. Otherwise, we don't know what you know, and don't know how much to bring you up to speed before we can answer your question.

Henry
+Pie Number of slices to send: Send
Sure. Here is my understanding. In, getLongWords(Collection<E> coll) method, getLongWords takes a Collection generic reference type when somebody calls this method.

We are able to declare Collection<E> as parameter because we defined <E extends CharSequence> after public.

Question 1: Why we cannot say, coll.add("Hello"); within the method even though String implements CharSequence interface

Question 2: Difference between Collection<E> and Collection before getLongWords in the 1st statement ? I am sure one is with and another one is with out generics..I am trying to see the possible effect/ impact with in the method.

Question 3: Difference between Collection<? extends CharSequence> and Collection before getLongWords in the 2nd statement ?.

Question 4 :In the 2nd statement, How can we use 2 similar things at the same time and possible effect in the method<? extends CharSequence> and <E extends CharSequence>
+Pie Number of slices to send: Send
 

Harikrishna Gorrepati wrote:Sure. Here is my understanding. In, getLongWords(Collection<E> coll) method, getLongWords takes a Collection generic reference type when somebody calls this method.

We are able to declare Collection<E> as parameter because we defined <E extends CharSequence> after public.

Question 1: Why we cannot say, coll.add("Hello"); within the method even though String implements CharSequence interface




You have to understand that Collection<E> where E is a type that extends CharSequence does *NOT* mean that collection can take any type of object that extend/implements CharSequence.

What the collection can take/hold is very specific -- it is a particular class type. Saying that E extends CharSequence doesn't expand on what the collection is allowed to hold. What that restriction does is limit on the type of collection that can be passed to the method. For example, the compiler will not allow you to pass a Collection<Integer> to the method.

So, the compiler doesn't know what the collection is allowed to hold, except that everything in the collection IS-A CharSequence. To answer your question, a String IS-A CharSequence, and E IS-A CharSequence, but the compiler can't confirm that a String IS-A E, and hence, won't allow you to add to the collection.

Henry


PS... BTW, not sure what you are asking with the other questions? Please provide some examples of what you mean.


+Pie Number of slices to send: Send
Hi,

Can I assume what you are trying is that you are making the return type of getLongWords method non-generic in Q2 and Q3?

If that is the case, you are loosing the Type Safety which is the key thing trying to achieve by means of java generics. Because the method can return any type of collection.

Thanks,
Kushan
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 821 times.
Similar Threads
Question about generics
Generics doubt
Ultimate generics Q
Generics problem
scjp 1.5 question related to collections
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:08:24.