• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics: Having two possible types  RSS feed

 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christopher Schneider
Greenhorn
Posts: 11
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!