Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #770
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Joshua Cranmer <Pidgeot18@verizon.invalid> |
| Newsgroups | comp.lang.java.help |
| Subject | Re: generics |
| Date | Tue, 14 Jun 2011 01:48:25 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 44 |
| Message-ID | <it6sn9$nuj$1@dont-email.me> (permalink) |
| References | <AqGdnSMWq6oC5mvQnZ2dnUVZ7rOdnZ2d@brightview.co.uk> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Tue, 14 Jun 2011 05:48:25 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="bAymlyY9SkaJNa8Tz2rerw"; logging-data="24531"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+3OMDA38iILVvxxZm2dIv3ObXxxc8+4Ag=" |
| User-Agent | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.16pre) Gecko/20110305 Lightning/1.0b3pre Thunderbird/3.1.10pre |
| In-Reply-To | <AqGdnSMWq6oC5mvQnZ2dnUVZ7rOdnZ2d@brightview.co.uk> |
| Cancel-Lock | sha1:LRQbkxL5kZWJXIO8Js7Ig6xSRzo= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.help:770 |
Show key headers only | View raw
On 06/13/2011 05:05 PM, Neil Morris wrote: > Cage<Animal> animalCage=new Cage<animal> > > Cage<Lion> lionCage=new Cage<lion> > > Cage<cat> catCage=new Cage<cat> > > > with the above definitions Cage is a subclass of Collection, but with > generics as i understand it the various cages ie Cage<animal> Cage<lion> > and Cage<cat> are not interchangable as below > > > lionCage.add(catCage)// not allowed!!! even though lionCage and castCage > are subclasses of Collection > > > could this be because that the various Cages take differant parameters? To give a fuller explanation: The purpose of generics (at least, so far as you are using them) is to declare what is being held by a container. So, in English terms, a "Cage<Animal>" is a cage that may hold an animal. Logically speaking, any time you put something into that cage or take it out, it must be something that is of type Animal. A "Cage<Lion>" is a cage that may hold a lion, any lion. But even though a lion is an animal, a Cage<Lion> is not a Cage<Animal>: I can put, say, a Penguin in a Cage<Animal>, but I can't put a Penguin in a Cage<Lion>; clearly, the two types must be distinct. Thus generified types do not follow the type hierarchy with respect to generic type parameters. As a final note, if you really do want a case where you want to store a Cage<Lion> into something that is "like" a Cage<Animal>, you can use wildcards like so: Cage<? extends Animal>, or, in plain English, a cage that holds things that are animals. The rules of Java work out (this is a bit of an oversimplification) so that you can't put anything into a Cage<? extends Animal>, but you know that everything you take out of one must be some sort of Animal. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
generics Neil Morris <neil.morris4@googlemail.com> - 2011-06-13 22:05 +0100
Re: generics markspace <-@.> - 2011-06-13 17:49 -0700
Re: generics Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-14 01:48 -0400
Re: generics Ian Shef <invalid@avoiding.spam> - 2011-06-14 19:45 +0000
Re: generics Neil Morris <neil.morris4@googlemail.com> - 2011-06-14 21:31 +0100
Re: generics markspace <-@.> - 2011-06-14 14:21 -0700
Re: generics Lewis Bloch <lewisbloch@google.com> - 2011-06-16 07:05 -0700
Re: generics Patricia Shanahan <pats@acm.org> - 2011-06-16 11:40 -0700
Re: generics Gene Wirchenko <genew@ocis.net> - 2011-06-16 15:14 -0700
csiph-web