Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is this a legal Generic method definition ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is this a legal Generic method definition ?" Watch "Is this a legal Generic method definition ?" New topic
Author

Is this a legal Generic method definition ?

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
How about this one ?

public static <E extends CharSequence> Collection<E super String> getLongWords( Collection<E> coll ){ }

If it is legal, then String would work.

How about this one ?

public static <E> Collection<E extends Number> getLongInts( Collection<E> nums ){ }

If it is legal, then Number and Integer and Long and Short and Byte would all work.


Thanks,
Harry
Sanjay Singh
Ranch Hand

Joined: Mar 02, 2006
Posts: 37

The following should be correct implementation:


and


Regards, Sanjay Singh
SCJP-1.6, OCEWCD 6
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

<E> is a user-defined typing here........... it can't be used on the Left Hand Side of extends or super keyword....

Think of it..
can you declare a generic like <Integer extends Object> or <String super Integer>..
you can't do so........

in the same way your declarations are also wrong...........


KARTHICK.C , SCJP6-93%
(Born to Win)
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
karthick chinnathambi wrote:<E> is a user-defined typing here........... it can't be used on the Left Hand Side of extends or super keyword....


Karthick,

My question isn't whether or not the following is correct, because I know that it is correct.

Sanjay Singh wrote:The following should be correct implementation:

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


My question is whether or not the following is correct, because I'm not sure about this declaration.

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

Thanks,
Harry
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

the implementation is not correct.
that is what i have explained above my dear friend.........
i suppose that you haven't understood my explanation......
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196



My question is whether or not the following is correct, because I'm not sure about this declaration.

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



you have declared E as a anything that extends char sequence... public static <E extends CharSequence>.......

again in the implementation part Collection<E extends CharSequence> is wrong here because you can't again re-declare the type here.......(this is just like ) which definitely produces compiler error........

i hope that it's clear now..........
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Thanks for your reply karthick. Just to clarify, I ran the following code. In this case,
<Object extends Object> is okay. But, I do get your point.

Collection<Object extends Object> is not correct.



C:\TEMP>java Tester
In generic subroutine
In main

karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

hi friend....

consider a generic declaration



<Object extends Object> is illegal because Object is NOT a legal identifier (KEYWORDS mustn't be used as identifiers)
but

Collection<? extends Object> can be assigned to Collection<Object> since Object "IS-A" Object......

feel free to ask doubts......... all our friends will help.....
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Hi Friend, I have a feeling that we are probably in agreement, but that we are having a problem communicating our ideas. -Harry



C:\TEMP>javac Tester.java
Tester.java:15: > expected
Collection<E extends Object> getLongWords( Collection<E> coll )
^
Tester.java:15: '(' expected
Collection<E extends Object> getLongWords( Collection<E> coll )
^
Tester.java:15: illegal start of type
Collection<E extends Object> getLongWords( Collection<E> coll )
^
3 errors




C:\TEMP>javac Tester.java
Tester.java:8: incompatible types
found : java.util.Collection<capture#240 of ? extends java.lang.Object>
required: java.util.Collection<java.lang.Object>
Collection<Object> o1 = new Tester().getLongWords( o );
^
1 error

karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196


{
......
Collection<Object> o1 = new Tester().getLongWords( o );
.....
}

public static <E extends Object> Collection<? extends Object> getLongWords( Collection<E> coll ) {}



see,

here you are invoking new Tester().getLongWorgs(0); whicj\h has a return type---> Collection<? extends Object>
The returned object is assigned to Collection<Object>




The above code says why it gave compiler error for your second program.........

your first program implements generics in a wrong way......... i have explained it before.........

Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Here what your method should look like:

public static <Declare new Generic type> Collection<Type of return value> getLongWords( Collection<Type of parameter> coll ){ }

now look what you've got:

<E extends CharSequence> - type declaration ok
Collection<E super String> - another type declaration? it is not the right place to declare new type where the return type should go
it would be legal to put here <? super String> though, as that is what type wildcard looks like
Collection<E> - type of parameter is ok, it was previously declared


<E> - type declaratin is ok
Collection<E extends Number> - one more type declaration where it should not occur
it would be legal to put here <? extends Number> though, as that is what type wildcard looks like
Collection<E> - valid parameter type, which was declared first.

PS. And don't forget that since you declared return type, you should return something from both methods...

[SCJP 6.0]
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Anastasia Sirotenko wrote:

karthick chinnathambi wrote:


Thank you. I understand what you both are saying to me, now. The code below compiles and runs. -Harry



C:\TEMP>java Tester
In generic subroutine
In main
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

thanks..........

welcome.......
 
 
subject: Is this a legal Generic method definition ?
 
Similar Threads
Question about generics
Need help in generics
Generics and More Generics...
Collections and Generics Help
Ultimate generics Q