redho home | products | services
Web Design Forums

Web Design Forums  


Web Hosting, Web Design, Software and Web Development Forums  
 FAQFAQ   MemberlistArchive  Log inLog in   RegisterRegister 
         

adding generics


Goto page 1, 2  Next
 
Post new topic   Reply to topic    Web Design Forums -> JAVA programming forum
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?
  Reply with quote


Starwarrior



Joined: 08 Jul 2006
Posts: 1
Because a container of a superclass is not a superclass of a container of a subclass.
  Reply with quote
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?
  Reply with quote
Ikopar



Joined: 26 May 2006
Posts: 168
No
  Reply with quote
Poolkop



Joined: 01 Jun 2006
Posts: 123
Aaron Zapashniy, generics is http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html and the generics tutorial is http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf and http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
  Reply with quote
Aaron Zapashniy



Joined: 04 Jun 2006
Posts: 17
Location: Hungary
Hm. odd. how useful are these things then?
  Reply with quote
amoralis



Joined: 22 Jun 2006
Posts: 21
How about List<Parent> list = new List<Child>(); doIt(list); ?
Is that still invalid?
  Reply with quote
Ikopar



Joined: 26 May 2006
Posts: 168
Yep
  Reply with quote
amoralis



Joined: 22 Jun 2006
Posts: 21
<- doesn't get to use generics
  Reply with quote
Ikopar



Joined: 26 May 2006
Posts: 168
It's unnecessary too
  Reply with quote
amoralis



Joined: 22 Jun 2006
Posts: 21
It seems like it should be valid, please explain why it isn't valid
  Reply with quote


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
  Reply with quote
amoralis



Joined: 22 Jun 2006
Posts: 21
Ikopar: perhaps in that list instance you don't want parent to be accepted
  Reply with quote
Ikopar



Joined: 26 May 2006
Posts: 168
Read the generics FAQ or tutorial, they cover this topic much better than I could
  Reply with quote
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
  Reply with quote
Ikopar



Joined: 26 May 2006
Posts: 168
You generally don't want that
  Reply with quote
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.
  Reply with quote
amoralis



Joined: 22 Jun 2006
Posts: 21
Generics in java kinda suck
  Reply with quote
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.
  Reply with quote
Gladis



Joined: 24 May 2006
Posts: 108
"templates" would be a better word for them.
Except that was taken. By something GOOD.
  Reply with quote
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.
  Reply with quote


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.
  Reply with quote
Gladis



Joined: 24 May 2006
Posts: 108
Aaron Zapashniy, std::vector<wookie>; <- I would call that templates.
  Reply with quote
Aaron Zapashniy



Joined: 04 Jun 2006
Posts: 17
Location: Hungary
I would ask how you got an STD from a wookie Smile
  Reply with quote
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
  Reply with quote
Page 1 of 2 Goto page 1, 2  Next
Post new topic   Reply to topic    Web Design Forums -> JAVA programming forum


Dubai Forums - Expat Help | Vegan Chat | Java Programming | Free 3D tutorials and 3d textures | Paris Forum | EU Forum
Free Dubai Classifieds | Free London Classifieds | Jobs in London

High Quality, Custom 3d animation and Web Design solutions Royal Quality Web Hosting Services Vegetarian and Animal Rights news

Powered by phpBB © 2001, 2005 phpBB Group