Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.palinacquisition!news.posted.palinacquisition.POSTED!not-for-mail NNTP-Posting-Date: Sat, 17 Dec 2011 13:33:50 -0600 Date: Sat, 17 Dec 2011 11:33:50 -0800 From: Peter Duniho User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Generics ? References: <2eOdnXdWwdSD2HHTnZ2dnUVZ_t6dnZ2d@posted.palinacquisition> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <_5Sdnag2-IwDc3HTnZ2dnUVZ_qqdnZ2d@posted.palinacquisition> Lines: 44 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 50.46.118.188 X-Trace: sv3-G6071Go/kOXcTMhxmS2eSTf2Zud344V76awRWtEvbsM/H/GEsdmVtn/7GvIwoJgy/n8EtU6rh/a5FXm!6T2fjFcy3urI8Ku3F/2euGjmHeaJYuNjNCRHWWuVHbr6pVKYSxJgTnbAsV1Fpibe8xxl9hdu0xTy!yGsjyn/QqjfdACkLnTJGJ8a+ZgNk/JECYHnnzfr0jjA= X-Complaints-To: abuse@iinet.com X-DMCA-Complaints-To: abuse@iinet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3368 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10835 On 12/17/11 10:20 AM, Knute Johnson wrote: > [...] > Example #4 Roedy's first suggestion compiles without warning but it is > no longer generic. Which brings me back to my real question, can you > extend a generic class and still be generic? As "markspace" says: of course you can. But you have to do it correctly. (It also helps if you don't keep your code examples secret, such as having a statement that calls "model.addElement()" that is important to you, but which you don't actually share with the rest of us). None of your examples actually followed the examples I provided precisely. In each case, you made a mistake: • example #1: you tried to declare a class name as if it had a generic type parameter, but used "String" as if it were the name of the parameter. • example #2: the declaration of the class is fine, but then in your code you made the illegal assumption that the type parameter "E" is actually "String", by trying to pass an instance of String to the addElement() method. • example #3: you failed to specify "String" as the type parameter for the base class, meaning you're extending it without generic support, hence the "unchecked" warning. • example #4: you can't make a class generic, but at the same time fix the type parameter as something specific. Frankly, I think example #4 is the most likely to be what you want. You appear to want to explicitly pass a string literal to the supporting types within your own derived class. If that's the case, then you have made the choice of the type parameter already. It _has_ to be String. If you actually want to declare a generic type yourself (e.g. so that you can reuse it generically), then you are not permitted to make any assumptions about the type parameter's value. That includes not passing string literals to a generic member, such as your example of the call to addElement(). Pete