Generics and polymorphism

Hi everybody,

Could anybody give me some info (maybe some good links about generics)

why I can't do something like this:

class Parent{}

class Childextends Parent{}

List<Parent> myList =new ArrayList<Child>();

[498 byte] By [AdrianSosialuka] at [2007-11-27 1:39:42]
# 1
[url http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf]generics-tutorial[/url][url http://java.sun.com/docs/books/tutorial/java/generics/index.html]Lesson: Generics[/url]There is also a seperate forum rught here if you'd scroll down the list of forumns.
abillconsla at 2007-7-12 0:53:06 > top of Java-index,Java Essentials,New To Java...
# 2

class Child2 extends Parent {}

You can add a Child2 object to a List < Parent> but not to a (Array)List < Child >.

Covariant/Contravariant functorality issues and all that.

DrLaszloJamfa at 2007-7-12 0:53:06 > top of Java-index,Java Essentials,New To Java...
# 3
This question is brought up on [url http://java.sun.com/docs/books/tutorial/java/generics/QandE/generics-answers.html] page [/url]. While Child is a sub-type of Parent, a "Collection" of Parent is not the same as a "Collection" of Child.
abillconsla at 2007-7-12 0:53:06 > top of Java-index,Java Essentials,New To Java...
# 4
Thank you very much for all your answers.I found some very good info in a book as well.Sorry for bringing up this question here - next time I willdouble check whether there is a specific forum for my question ;)Cheers,Adrian
AdrianSosialuka at 2007-7-12 0:53:06 > top of Java-index,Java Essentials,New To Java...
# 5
Sure thing.
abillconsla at 2007-7-12 0:53:06 > top of Java-index,Java Essentials,New To Java...