Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

List<Cat> cannot be a subclass of List<Animal> as that would imply I can do list.add(dog) into that list.

You probably know this, but I'm replying in the hope it clarifies things to other readers. The way it's expressed in Java is List<? extends Animal>



Yeah, java can change the variance model used by generics, but my point is that it is a language-level feature, not something fundamental at a JVM level, which is imo the correct decision.

Also, unfortunately arrays are covariant so Cat[] is a subclass of Animal[] (both in Java and C# actually), where your mentioned example indeed introduces a “poisoned” value, waiting for a classcastexception.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: