• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics  RSS feed

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Please advice what is the difference between the following 2 statements..
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Kushan Athukorala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!