Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #770

Re: generics

From Joshua Cranmer <Pidgeot18@verizon.invalid>
Newsgroups comp.lang.java.help
Subject Re: generics
Date 2011-06-14 01:48 -0400
Organization A noiseless patient Spider
Message-ID <it6sn9$nuj$1@dont-email.me> (permalink)
References <AqGdnSMWq6oC5mvQnZ2dnUVZ7rOdnZ2d@brightview.co.uk>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar


Thread

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