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


Groups > comp.lang.java.programmer > #10836

Re: Generics ?

From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: Generics ?
Date 2011-12-17 11:40 -0800
Organization A noiseless patient Spider
Message-ID <jcir75$2jb$1@dont-email.me> (permalink)
References (1 earlier) <ag5me7h7pph1d4n2b05uvgump03dp7ig0t@4ax.com> <jchb74$n8m$1@dont-email.me> <2eOdnXdWwdSD2HHTnZ2dnUVZ_t6dnZ2d@posted.palinacquisition> <jcimia$9dh$1@dont-email.me> <_5Sdnag2-IwDc3HTnZ2dnUVZ_qqdnZ2d@posted.palinacquisition>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-15 21:48 -0800
  Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-15 22:03 -0800
    Re: Generics ? Tassilo Horn <tassilo@member.fsf.org> - 2011-12-16 08:30 +0100
      Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-16 00:08 -0800
  Re: Generics ? Roedy Green <see_website@mindprod.com.invalid> - 2011-12-16 02:10 -0800
    Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-16 22:01 -0800
      Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-16 23:30 -0800
        Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 10:20 -0800
          Re: Generics ? markspace <-@.> - 2011-12-17 10:54 -0800
            Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:25 -0800
              Re: Generics ? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-17 21:20 -0800
                Re: Generics ? "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-12-18 21:08 +1100
                Re: Generics ? Lew <lewbloch@gmail.com> - 2011-12-18 08:17 -0800
                Re: Generics ? markspace <-@.> - 2011-12-18 08:43 -0800
          Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-17 11:33 -0800
            Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:40 -0800
              Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-17 11:47 -0800
              Re: Generics ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-17 15:13 -0500
      Re: Generics ? Roedy Green <see_website@mindprod.com.invalid> - 2011-12-17 02:46 -0800
  Re: Generics ? "John B. Matthews" <nospam@nospam.invalid> - 2011-12-17 11:18 -0500
    Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:16 -0800

csiph-web