• 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

Generic question

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given that CharSequence is an Interface implementing by both String and String Builder

and then given the following method :



why 3 not ?

from K&B
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because S is not necessarily a String.
 
Arad Chear
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes but String implements CharSequence

so its allowed since return type is <S extends CharSequence>
 
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 Eisa,

YOu are right, but I think In this type of questions when we are given a generic method, we have to preserve the generic nature of the method and not tie it down to something specific.

String is good and so are StringBuilder, StringBuffer. But this method remains generic(or general which can be used by all types which statisfy the type defination) only if we return either the type defined ie s or null, since null is a member of all non-primitive types.
 
Arad Chear
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




you're righ joshi its generic method , nut in s.toString its always return string regardless what is S ,

sorry guys , but i still confused
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try one more time

See s.toString()....what does it give ??? You are right ...String.

Now the method returns S and takes argument S....

By returning toString() ...we made this method only accept and return String....now we can't return StringBuffer object...Can't return StringBuilder object .... bcoz we locked down this methods generic defination to String...

That is exactly why we can't return toString()...

Hope this helps...
[ April 23, 2007: Message edited by: megha joshi ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eisa,

You think this example in your own way:


So in this case if the method only returns Banana, it will go wrong;
You have four cases to resolve, means method must return what is passed
to it. So how to do so

That is why if you write:

return s; //Always true
//Because what is S can simply be returned because return type of the
method is S. Forget what is S (Apple,Banana,Orange,Fruit), if you simply return s, it will do

And let us come to null (I think you understand it better);

Anyways,
null can be assigned to any type of reference variable, so therefore returning null has no botheration.




"Remember, CharSequence is an Interface"

Now I hope you got the scenario!!!

Regards,
cmbhatt
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also don't get it...
What is wrong with this code?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sasha,



Thats is because T can be Daughter, GrandDaughter, GrandSon and so on. And how can you be sure T is only Son? That is why it is not allowed.

You can be right in case:


See one more case:



Regards,
cmbhatt
[ April 24, 2007: Message edited by: Chandra Bhatt ]
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How could I ever return T in this scenario? T is not a type!
Does that mean a method defined as public <T extends Father > T foo(){... is only allowed to return null? At least nothing else seems to work. Strange...
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sasha,

Have a look at the given code! Inside the method we are forced to return T
type only if method says that it returns. Nothing else. We don't know what
may be the T.







cmbhatt
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra, thanks for your reply.
I have absolutely no problem with your code, as you have a generic class everything works fine. But my question is: In a normal class, given a method with only generic <T extends MyClass> return-type (see above) is there any chance of returning anything other than null? I have tried many possibilities but only null seems to work.
So I tend to see it this way: If you have made such a method declaration you sign a contract that says: Whatever the returned value will be, I assure that you can store it in any reference-variable of any subtype of MyClass. And obviously this works only for null (given two classes Son and Daughter that extends MyClass, the only value that a variable of type Son and a variable of type Daughter are able to store is... null)
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sasha,

Exception to what you said regarding "only null".

See the code:




Is that what you wanted?



Regards,
cmbhatt
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra that's not fair. I just thought that i understand this whole return of a generic type thing, and now you come with this code and I am totally confused.

Compiling your code I get the compiler-warning:
"Type safety: The cast from Son to T is actually checking against the erased type Father"

But yes, it works. Although I am totally unsure why

And I am unsure whether it is necessary to understand for the exam, as something like this behavior is not discussed in the K&B book. Anyway, I will get back to your code when I am redoing the generics stuff. At the moment I am engaged with the Threads-chapter. And thanks again for posting this code, it's interesting!
reply
    Bookmark Topic Watch Topic
  • New Topic