• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in generics

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I have Sun certified programmer for java 5 of Kathy-Bates.
In 'Self test' of 'Generics and Collections' Charpter 7. Question 14 there are some things which i did not understood properly. Code is as follow-

Questions are as follow -
1.If <E extends CharSequence> is same as <? extends CharSequence>, how come longWords.add(word) can be used. If collection type contains extends then you cannot use add method. Here how it is possible?
2.Why i cannot say in getLongWords() as follow?
Collection<E> longWords = new ArrayList<String>();
As String implements CharSequence, it should work fine here. But i get compile time error.
3.How come there is compile time error on line
Collection<String> resultList = getLongWords(list);
If i change the method declaration as follow ?
static public <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll) {
4.Will there be questions in SCJP cx-310-055 on generics combined with overriding?
5.Please tell me which book is good for generics from the point of view of understanding and explaination?
Please help me!
Thanks in advance
Sharmila
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. "If collection type contains extends then you cannot use add method"..is not right. You cannot use add when you are using the wild card character '?' without super.

2. You are using E as unknown type for a generic method..

3. "If i change the method declaration as follow ?
static public <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll) { "

Here you are returning Collection<? extends CharSequence>

So.. if you do the above and do

Collection<String> resultList = getLongWords(list);

It means

Collection<String> resultList can have anything that extends CharSequence, implying you are trying to use extends in the RHS of the expression which is not leagal either.

but if you do

Collection resultList = getLongWords(list); (without generics) ..it works fine


4. Exam can have any legal questions..but they wont be complex to solve. The question you have asked now can be classified in expert level

5. K&B should be sufficient, but Khalid Mughal seems to me a nice book too. I think studying one of them couple of times shud work.

This Generics Mock I have found useful to improve my knowledge.

All the best with your preparation


 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharmila,


1.If <E extends CharSequence> is same as <? extends CharSequence>, how come longWords.add(word) can be used. If collection type contains extends then you cannot use add method. Here how it is possible?



The <E extends CharSequence> is the definition of the generic variable that you would be using to define a generic method.
for ex:

in the above generic method there is no restriction on what T can be.
but in the following code:

there is a restriction on what T can be. T should pass an IS-A test for CharSequence.

The point to note here is that <E extends CharSequence> is the definition of the variable that you would be using while implementing your definition method, but notice that the "actual" method definition has no "extends" keyword in its signature. So at runtime the type is locked and since you are adding the same type to the collection there would be no exception.


2.Why i cannot say in getLongWords() as follow?
Collection<E> longWords = new ArrayList<String>();
As String implements CharSequence, it should work fine here. But i get compile time error.



Lets say E is a "StringBuffer" during run time. Now can you say?

Because StringBuffer and String are peers though they both implement CharSequence per se. So the compiler senses this danger before hand and wouldn't compile.


3.How come there is compile time error on line
Collection<String> resultList = getLongWords(list);
If i change the method declaration as follow ?
static public <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll) {



Because you are expecting a String collection but returning some collection "whose members extend(implement) CharSequence". No guarantee that it would be a collection of Strings! you can can return a StringBuffer collection from the method but you are trying to assign it to a String collection. Compiler doesn't agree.


5.Please tell me which book is good for generics from the point of view of understanding and explaination?
Please help me!



K&B book proved sufficient for me. Every time I read I learn a new point. Try reading it as many times as possible though you feel comfortable already.

Hope this helps.

-Reuel.


 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Himalaya,
Thanks for your reply, How to classify question at expert lever?
I will read more on generics till my concept are clear.
Thanks
Sharmila
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reuel,
Thanks for your help.
That is true, i need to read it many times.
Thanks,
Sharmila
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In some mocks(like whizlab) they mention if it is beginner, intermediate or expert. There is no hard and fast rule to classify..just to give you an idea I classified it here.
From exam point of view..I wud say..Instead of reading many things in Generics..just reading K&B and taking lot of mocks is sufficient.

Himalay
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Himilay,
Thanks,
Will solve mocks so that i can see many more tricky questions.
Sharmila
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both the time you have spelled my name with syntax error..

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic