wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic question" Watch "Generic question" New topic
Author

Generic question

Arad Chear
Ranch Hand

Joined: Jan 05, 2007
Posts: 98
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
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Because S is not necessarily a String.
Arad Chear
Ranch Hand

Joined: Jan 05, 2007
Posts: 98
yes but String implements CharSequence

so its allowed since return type is <S extends CharSequence>
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
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

Joined: Jan 05, 2007
Posts: 98




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

Joined: Feb 20, 2007
Posts: 206
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 ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
I also don't get it...
What is wrong with this code?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Mar 29, 2007
Posts: 115
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Mar 29, 2007
Posts: 115
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

Joined: Feb 28, 2007
Posts: 1707
Sasha,

Exception to what you said regarding "only null".

See the code:




Is that what you wanted?



Regards,
cmbhatt
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic question
 
Similar Threads
Doubt in Generics. pl help
Generics Doubt
Question on using return value and not.
S extends CharSequence
Generics return type