| Author |
generics error
|
karthik swamy
Ranch Hand
Joined: Mar 14, 2011
Posts: 37
|
|
i am getting this error after compiling :
so please suggest me where i am wrong.
Thanks,
karthik swamy.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
your *public T getData() * expect the generic return type T . but you have returned a string!
so change the method return type as String[preferable approach] else cast the string to T and return as in *return (T)s*;
<edit>added clarity</edit>
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
and please use code tag when you post code!
|
 |
karthik swamy
Ranch Hand
Joined: Mar 14, 2011
Posts: 37
|
|
Seetharaman Venkatasamy wrote:your *public T getData() * expect the generic return type T . but you have returned a string!
so change the method return type as String[preferable approach] else cast the string to T and return as in *return (T)s*;
<edit>added clarity</edit>
thanks its working,but can it wil work for integer type
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
karthik swamy wrote:but can it wil work for integer type
actually *return (T)s* this lead you in confusion. that is why I said first approach is preferable. Yes , it will work for an Integer type also
|
 |
karthik swamy
Ranch Hand
Joined: Mar 14, 2011
Posts: 37
|
|
Seetharaman Venkatasamy wrote:
karthik swamy wrote:but can it wil work for integer type
actually *return (T)s* this lead you in confusion. that is why I said first approach is preferable. Yes , it will work for an Integer type also 
but generics is used for auto typecast then why we are typecasting like return (t)s
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
karthik swamy wrote:but generics is used for auto typecast then why we are typecasting like return (t)s
your T getData implementation force us to do that! you should reimplement[even signature of method] the method!
|
 |
 |
|
|
subject: generics error
|
|
|