| View previous topic :: View next topic |
| Author |
Message |
Aaron Zapashniy
Joined: 04 Jun 2006 Posts: 17 Location: Hungary
|
| Hi. i'm starting to add generics to a lot of my 1.4 code. i have a utility method which takes in a container of SuperClass. i'm now getting compile errors when i pass it a container of Subclass. why? |
| |
|
|
|
|
Starwarrior
Joined: 08 Jul 2006 Posts: 1
|
| Because a container of a superclass is not a superclass of a container of a subclass. |
| |
|
|
Aaron Zapashniy
Joined: 04 Jun 2006 Posts: 17 Location: Hungary
|
I.e. void doIt(List<Parent>){ ...} ... List<Child> list = new List<Child>(); doIt(list);
That's not legal? |
| |
|
|
Ikopar
Joined: 26 May 2006 Posts: 168
|
| No |
| |
|
|
Poolkop
Joined: 01 Jun 2006 Posts: 123
| |
Aaron Zapashniy
Joined: 04 Jun 2006 Posts: 17 Location: Hungary
|
| Hm. odd. how useful are these things then? |
| |
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
How about List<Parent> list = new List<Child>(); doIt(list); ?
Is that still invalid? |
| |
|
|
Ikopar
Joined: 26 May 2006 Posts: 168
|
| Yep |
| |
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
| <- doesn't get to use generics |
| |
|
|
Ikopar
Joined: 26 May 2006 Posts: 168
|
| It's unnecessary too |
| |
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
| It seems like it should be valid, please explain why it isn't valid |
| |
|
|
|
|
Ikopar
Joined: 26 May 2006 Posts: 168
|
List<Parent> list = new ArrayList<Parent>(); list.add(instanceOfChild);
Because generics don't work that way, there's no inheritence |
| |
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
| Ikopar: perhaps in that list instance you don't want parent to be accepted |
| |
|
|
Ikopar
Joined: 26 May 2006 Posts: 168
|
| Read the generics FAQ or tutorial, they cover this topic much better than I could |
| |
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
Perhaps you only want a specific child object there
Not that I have a use case but maybe Aaron Zapashniy does heh |
| |
|
|
Ikopar
Joined: 26 May 2006 Posts: 168
|
| You generally don't want that |
| |
|
|
Aaron Zapashniy
Joined: 04 Jun 2006 Posts: 17 Location: Hungary
|
I see. hm. interesting. okay.
Should still work fine, as 99% of the time i think i'm only performing parent methods when acting on collections. |
| |
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
| Generics in java kinda suck |
| |
|
|
Aaron Zapashniy
Joined: 04 Jun 2006 Posts: 17 Location: Hungary
|
Is it just me or does the term generics seem completely contrary to what they actually are?
They're not generics. they're specifics.
Treating things as Object is generic. |
| |
|
|
Gladis
Joined: 24 May 2006 Posts: 108
|
"templates" would be a better word for them.
Except that was taken. By something GOOD. |
| |
|
|
Aaron Zapashniy
Joined: 04 Jun 2006 Posts: 17 Location: Hungary
|
| To me, List list = new ArrayList(); is a GENERIC list, and List<Wookie> = new ArrayList<Wookie>(); is ..well i wouldn't call it a Template either but it's not generic. |
| |
|
|
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
| If you passed in a List of Parent that was populated by all child classes that would not introduce any problems, but as soon as you try to passing a generified list parameterized to child into a method requiring a list parameterized as parent it has issues... that's stupid. It would not cause any problems. |
| |
|
|
Gladis
Joined: 24 May 2006 Posts: 108
|
| Aaron Zapashniy, std::vector<wookie>; <- I would call that templates. |
| |
|
|
Aaron Zapashniy
Joined: 04 Jun 2006 Posts: 17 Location: Hungary
|
I would ask how you got an STD from a wookie  |
| |
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
| Gladis, I think the reason for the name difference is that in C++ it effects code outcome, in java it doesn't |
| |
|
|
| Page 1 of 2 |
Goto page 1, 2 Next |
|