aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics and More Generics... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics and More Generics..." Watch "Generics and More Generics..." New topic
Author

Generics and More Generics...

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hi!
These are codes from K&B.
Can some body tell me how to approach and understand these questions.
They are look sooooo confusing.
And I always get messed up with such questions with so many code.
And half the time I dn't even undersand the question..let alone the answer.

So if any one has any tips on how to handle these questions..please Help!




Q1.

Given that String implements java.lang.CharSequence


Which when inserted would compile and run.

A. public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)

B. public static <E extends CharSequence> List<E> getLongWords(Collection<E> coll)

C. public static Collections<E extends CharSequence> getLongWords(Collection<E> coll)

D. public static List<? extends CharSequence> getLongWords(Collection<CharSequence> coll)

E. public static <E extends CharSequence> getLongWords(Collection<? extends CharSequence> coll)

F.static public <E extends CharSequence> Collection<E> getLongWords(<Collection<E> coll)

G.static public <E super charSequence> Collection<E> getLongWords(Collection<E> coll)


Answer is F






Q2.

Given a method declared as

public static <E extends Number> List<E> process (List<E> nums)


A prgrammer wants to use this method like this:

// INSER DECLARATION HERE

output = process(input);

Which pairs of the declarations should be placed to for the code to compile.?

A ArrayList<Integer> input=null;
ArrayList<Integer> output=null;

B. ArrayList<Integer> input=null
List<Integer> output=null;

C. ArrayList<Integer> input=null
List<Number> input=null;

D.List<Number> input=null;
ArrayList<Integer> output=null;

E List<Number> input=null;
List<Number> input=null;

F. List<Integer> input=null;
List<Integer> input=null

G.None of the above


Answer is B,E,F.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
I would answer to ya, but I'm going to college right now, tomorrow if no one has answered I'll.

try to understand until it :-)


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Okay I' ll try...
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

Hi
First of all the code you have given has lot of errors i guess it should look like this


1) You need to see generic parameter definition matches our method call or not.
Here <E extends CharSequence> matches to String as String extends from CharacterSequence.

2)and see method return type, matches to the variable which is taking the result of method call

Here Collection<String> resultList=getLongWords(list);

see resultList type matches with the return type of getLongWords(list)

3)And you need to see method parameter can cause any compile time errors in method body.


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
A. public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)


What is <E extends CharSequence> here?
Is it the return type?

If it is the return type then what is B]Collection<? extends CharSequence>[/B].I thought it meant the return type is of "Collection" which is of "? extends CharSequence" type.
In that case what doese <E extends CharSequence> before the Collection <............> mean.


They both look like return types to me , so how can they be placed together?

Can some one pleas help me to figure this out
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Nabila Mohammad:
A. public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)


What is <E extends CharSequence> here?
Is it the return type?

If it is the return type then what is B]Collection<? extends CharSequence>[/B].I thought it meant the return type is of "Collection" which is of "? extends CharSequence" type.
In that case what doese <E extends CharSequence> before the Collection <............> mean.


They both look like return types to me , so how can they be placed together?

Can some one pleas help me to figure this out


Sorry for not being able to answer the complete question. I'm at work and dont have much time now.
<E extends CharSequence> is creating a type and its being used on the parameters of the method in Collection<E>.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I still didn't get it

That's ok..I ll wait
Till you get time...whenever that is.. !
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
Originally posted by Nabila Mohammad:
A. public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)


What is <E extends CharSequence> here?
Is it the return type?

If it is the return type then what is B]Collection<? extends CharSequence>[/B].I thought it meant the return type is of "Collection" which is of "? extends CharSequence" type.
In that case what doese <E extends CharSequence> before the Collection <............> mean.


They both look like return types to me , so how can they be placed together?

Can some one pleas help me to figure this out


no it is not the return type of collection. It is a TYPE restriction on E. Thats how you define a Generic class.
"access specifier" <Type with its restrictions> "return type of the method" MethodName ( "Argument") {}

<E extends CharSequence> simply states that you can have E such that it extends CharSequence i.e. you can have String in place of E as it extends CharSequence but you cannot have Integer in place of E.

Hope it helps


SCJP 6 83%
Working on SCWCD/OCPJWCD
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hey Milan,

Thanks for the reply..

I got a part of it but not much.

Do you know any link i can look into which talks about Acess Specifier in Generics...

Thnks for your help..
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
as such there are no separate rules for generics.
if its a generic method /class / reference variable then it can have those access specifiers that a non-generic(legacy) method /class / reference variable would have if it were it in its place.

For more help refer to the links posted by me in this thread Here
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hey Milan.
Thanks for the link..

Looks like you have done a lot of reasearch on Generics..

Do you know any link which helps you to understand Generics better in a simple manner..

Still not clear with it..
So I ll do a little more bit of studying before shooting anymore questions..
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)

See, if you are looking for the generic type of the return type, look after the return type. After closely looking the method declaration, you'll see that a Collection object is returned(of the wildcard type that is a/is a subclass of CharSequence(like String).), but before that, the <E extends CharSequence> stands for the 'E type' parameter that is used in the method.

For your query about access specifier, I would recommend that you read the K&B section about them as they are explained in a lucid manner.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Sumit Bisht:
public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)

See, if you are looking for the generic type of the return type, look after the return type. After closely looking the method declaration, you'll see that a Collection object is returned(of the wildcard type that is a/is a subclass of CharSequence(like String).), but before that, the <E extends CharSequence> stands for the 'E type' parameter that is used in the method.

For your query about access specifier, I would recommend that you read the K&B section about them as they are explained in a lucid manner.



Okay , that means we are sort of defining the E used in getLongWords(Collection<E> coll)
and this E is being defined as <E extends CharSequence>

Wow ! that makes a whole lot of sense.
Looks like a major break through for me in Generics!
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks Sumit , you have been a big help!
 
wood burning stoves
 
subject: Generics and More Generics...