• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Is this a legal Generic method definition ?

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 37
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following should be correct implementation:


and
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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...........
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


{
......
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.........

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks..........

welcome.......
 
This tiny ad is guaranteed to be gluten free.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic