Forums Register Login

Generics: Having two possible types

+Pie Number of slices to send: Send
Hello,

I am getting confused by generics. Please ignore the really bad design of the code below, it's just for test purposes.

Basically, I am a bit confused here - I know you can specify more than one type in the class definition, but what if you want your variable to be able to be more than one type?

For example, what if I want to be able to put either a phone or a comb in my bag, how can I do this? I have tried the below, but I seem to be doing it all wrong.

Thanks.

1
+Pie Number of slices to send: Send
Generics don't work the way you appear to think they do. The names you define inside the angle brackets are type variables. Think of the generic class as kind of a template and as such it is going to be one level of abstraction higher than what you show in your code. You're confusing yourself even more by having a main method in your generic class.

Read through the tutorial again: https://docs.oracle.com/javase/tutorial/java/generics/types.html
1
+Pie Number of slices to send: Send
You can't specify multiple classes in a generic class (except maybe if you consider wildcards), but you could, for instance, specify an interface, then implement that interface in only the objects you want put in a bag.

+Pie Number of slices to send: Send
Thanks guys.

Actually, I had read that Box example from official documentation, but was still confused, but now read it again, and see where I am going wrong.

However, I still have an error in method getItemFromBag() - error: "The method getWhatAmI() is undefined for the class T".

Any ideas>

1
+Pie Number of slices to send: Send
 

John Smithonian wrote:
However, I still have an error in method getItemFromBag() - error: "The method getWhatAmI() is undefined for the class T".



Read Knute's post again. Consider creating an interface with the requested method -- and bound the generic type to the interface. This way, the Java compiler is assured that the getWhatAmI() method is guaranteed to exist.

Henry
1
+Pie Number of slices to send: Send
I think this may help. Looking at your MyGeneric class:



You should remember that T can be anything. With that code, I could theoretically do this:



There is no method getWhatAmI() defined for an Integer class, however, so you'll receive a compile time error.
+Pie Number of slices to send: Send
 

Christopher Schneider wrote:
You should remember that T can be anything. With that code, I could theoretically do this:



There is no method getWhatAmI() defined for an Integer class, however, so you'll receive a compile time error.



Of course - you are absolutely right. It makes sense now.

Thanks!
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 408 times.
Similar Threads
status of deamon threads
Generics
Overriden Vs Re-Define
character code
Doubt in generic method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:03:26.