Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: Generics ? Date: Sat, 17 Dec 2011 11:40:21 -0800 Organization: A noiseless patient Spider Lines: 52 Message-ID: References: <2eOdnXdWwdSD2HHTnZ2dnUVZ_t6dnZ2d@posted.palinacquisition> <_5Sdnag2-IwDc3HTnZ2dnUVZ_qqdnZ2d@posted.palinacquisition> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 17 Dec 2011 19:40:21 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="2667"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18zsfGodnFy5/rnT/wm5vZT" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <_5Sdnag2-IwDc3HTnZ2dnUVZ_qqdnZ2d@posted.palinacquisition> Cancel-Lock: sha1:uPvdvvomKijAuHzcrQBfHkzCAAA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10836 On 12/17/2011 11:33 AM, Peter Duniho wrote: > 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 Thanks Pete. That's obvious now but wasn't yesterday :-). You know I looked all over the net for examples of extending a generic class and found none. It could be that I didn't know what I was looking for really. -- Knute Johnson