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


Groups > comp.lang.java.programmer > #8677 > unrolled thread

What Does it Mean to Pass a Generic Class Itself as a Parameter?

Started byKevinSimonson <kvnsmnsn@hotmail.com>
First post2011-10-10 08:01 -0700
Last post2011-10-11 02:08 -0400
Articles 11 — 7 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  What Does it Mean to Pass a Generic Class Itself as a Parameter? KevinSimonson <kvnsmnsn@hotmail.com> - 2011-10-10 08:01 -0700
    Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Jeff Higgins <jeff@invalid.invalid> - 2011-10-10 12:11 -0400
      Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Jeff Higgins <jeff@invalid.invalid> - 2011-10-10 12:56 -0400
    Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? markspace <-@.> - 2011-10-10 09:12 -0700
      Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Patricia Shanahan <pats@acm.org> - 2011-10-10 09:31 -0700
        Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Lew <lewbloch@gmail.com> - 2011-10-10 12:12 -0700
          Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-10 13:32 -0700
            Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Jeff Higgins <jeff@invalid.invalid> - 2011-10-10 16:40 -0400
            Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Lew <lewbloch@gmail.com> - 2011-10-10 14:29 -0700
    Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Screamin Lord Byron <scre@min.dot> - 2011-10-10 19:07 +0200
    Re: What Does it Mean to Pass a Generic Class Itself as a Parameter? Jeff Higgins <jeff@invalid.invalid> - 2011-10-11 02:08 -0400

#8677 — What Does it Mean to Pass a Generic Class Itself as a Parameter?

FromKevinSimonson <kvnsmnsn@hotmail.com>
Date2011-10-10 08:01 -0700
SubjectWhat Does it Mean to Pass a Generic Class Itself as a Parameter?
Message-ID<c534ac30-345e-4467-ba44-5d719d8c22ca@6g2000prh.googlegroups.com>
I'm currently taking a class pretty much each week to prepare for the
SCJP exam.  In the classes we usually go over practice exams and try
to figure out what the right answer is to a number of the questions.
After the chapter on Generics, one of the questions involved a class
like the one I call {Wierd} here.  It was kind of of the form:

public class Wierd< T extends Wierd>
{
  public Wierd ()
  {
  }
}

and then we had a class of the form:

public class Hmm
{
  public static void main ( String[] arguments)
  {
    Wierd< Wierd> wow = new Wierd< Wierd>();
  }
}

Can anyone tell me what's going on here?  In {Wierd} itself is the
{Wierd} that's being extended in the angle brackets the same {Wierd}
that's being defined in this file?  And what exactly is happening in
{main()} when I declare {wow} to be a {Wierd< Wierd>} object?  I'd
really appreciate it if someone could explain this to me.

Kevin Simonson

[toc] | [next] | [standalone]


#8679

FromJeff Higgins <jeff@invalid.invalid>
Date2011-10-10 12:11 -0400
Message-ID<j6v5cf$d72$1@dont-email.me>
In reply to#8677
On 10/10/2011 11:01 AM, KevinSimonson wrote:
> public class Wierd<  T extends Wierd>
> {
>    public Wierd ()
>    {
>    }
> }
If you mean
class Foo< T extends Foo<T>>
you can search
java+generics+recursive+bounds
or
<http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206>

[toc] | [prev] | [next] | [standalone]


#8683

FromJeff Higgins <jeff@invalid.invalid>
Date2011-10-10 12:56 -0400
Message-ID<j6v81e$m6$1@dont-email.me>
In reply to#8679
On 10/10/2011 12:11 PM, Jeff Higgins wrote:
> On 10/10/2011 11:01 AM, KevinSimonson wrote:
>> public class Wierd< T extends Wierd>
>> {
>> public Wierd ()
>> {
>> }
>> }
> If you mean
> class Foo< T extends Foo<T>>
> you can search
> java+generics+recursive+bounds
> or
> <http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206>
>
or
<http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html#FAQ106>

[toc] | [prev] | [next] | [standalone]


#8680

Frommarkspace <-@.>
Date2011-10-10 09:12 -0700
Message-ID<j6v5ha$e7s$1@dont-email.me>
In reply to#8677
On 10/10/2011 8:01 AM, KevinSimonson wrote:
> Can anyone tell me what's going on here?  In {Wierd} itself is the
> {Wierd} that's being extended in the angle brackets the same {Wierd}
> that's being defined in this file?


Yes.  Extension, and polymorphism, just mean "type."  Weird is a type of 
Weird, so it fulfills the bound that the type of T must be a type of 
Weird (extends Weird).


> And what exactly is happening in
> {main()} when I declare {wow} to be a {Wierd<  Wierd>} object?


Well, you didn't do anything with the type parameter T, so not a lot is 
happening.  Try to find some existing classes that use type parameters, 
like the Collections classes, and ask that question for them.

[toc] | [prev] | [next] | [standalone]


#8681

FromPatricia Shanahan <pats@acm.org>
Date2011-10-10 09:31 -0700
Message-ID<TI2dnXg8ofRugA7TnZ2dnUVZ_rydnZ2d@earthlink.com>
In reply to#8680
On 10/10/2011 9:12 AM, markspace wrote:
> On 10/10/2011 8:01 AM, KevinSimonson wrote:
>> Can anyone tell me what's going on here? In {Wierd} itself is the
>> {Wierd} that's being extended in the angle brackets the same {Wierd}
>> that's being defined in this file?
>
>
> Yes. Extension, and polymorphism, just mean "type." Weird is a type of
> Weird, so it fulfills the bound that the type of T must be a type of
> Weird (extends Weird).
>
>
>> And what exactly is happening in
>> {main()} when I declare {wow} to be a {Wierd< Wierd>} object?
>
>
> Well, you didn't do anything with the type parameter T, so not a lot is
> happening. Try to find some existing classes that use type parameters,
> like the Collections classes, and ask that question for them.
>
>

java.lang.Class is also an interesting example. It is declared as
Class<T> where T is the type of the class modeled by the Class object.
Now think about the Class object for java.lang.Class. It is a Class<Class>.

Patricia

[toc] | [prev] | [next] | [standalone]


#8687

FromLew <lewbloch@gmail.com>
Date2011-10-10 12:12 -0700
Message-ID<10851131.278.1318273920911.JavaMail.geo-discussion-forums@prng5>
In reply to#8681
KevinSimonson wrote:
>>> Can anyone tell me what's going on here? In {Wierd} itself is the
>>> {Wierd} that's being extended in the angle brackets the same {Wierd}
>>> that's being defined in this file?

On a tangential note, it's best practice to make identifiers, such as your class name 'Wierd', either sufficiently obviously not intended to be a natural-language word that spelling is not an issue, or to spell it the same as the natural-language word it resembles.  So, for example, to model a boundary, 'bnd' would be an acceptable variable name, but 'boundray' would not be.  The reason is that it's too difficult for maintainers to get near-miss spelling correct - the pull to spell the variable or type name the same as the natural-language word is just too strong.

So fix the spelling of 'Wierd'.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#8689

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2011-10-10 13:32 -0700
Message-ID<XDIkq.3151$mH7.2957@newsfe10.iad>
In reply to#8687
On 10/10/11 12:12 PM, Lew wrote:
> KevinSimonson wrote:
>>>> Can anyone tell me what's going on here? In {Wierd} itself is the
>>>> {Wierd} that's being extended in the angle brackets the same {Wierd}
>>>> that's being defined in this file?
>
> On a tangential note, it's best practice to make identifiers, such as your class name 'Wierd', either sufficiently obviously not intended to be a natural-language word that spelling is not an issue, or to spell it the same as the natural-language word it resembles.  So, for example, to model a boundary, 'bnd' would be an acceptable variable name, but 'boundray' would not be.  The reason is that it's too difficult for maintainers to get near-miss spelling correct - the pull to spell the variable or type name the same as the natural-language word is just too strong.
>
> So fix the spelling of 'Wierd'.
>
Weird, IntelliJ IDEA will not only auto-suggest the correctly misspelled 
variant, but the latest version, with the Spell Correct plug in, will 
offer to rename the symbol in question to the correct spelling, taking 
into account Camel Case word boundaries and all.

Wow, talk about a run-on sentence. Oh well, the point is that tools are 
cool and help us flawed humans recover from many common mistakes.

[toc] | [prev] | [next] | [standalone]


#8690

FromJeff Higgins <jeff@invalid.invalid>
Date2011-10-10 16:40 -0400
Message-ID<j6vl5i$54s$1@dont-email.me>
In reply to#8689
On 10/10/2011 04:32 PM, Daniel Pitts wrote:

> Wow, talk about a run-on sentence. Oh well, the point is that tools are
> cool and help us flawed humans recover from many common mistakes.
Hammer with built-in nail puller. How cool is that? :)

[toc] | [prev] | [next] | [standalone]


#8691

FromLew <lewbloch@gmail.com>
Date2011-10-10 14:29 -0700
Message-ID<20002215.1699.1318282194149.JavaMail.geo-discussion-forums@prdw1>
In reply to#8689
Daniel Pitts wrote:
> Lew wrote:
>> KevinSimonson wrote:
>>>>> Can anyone tell me what's going on here? In {Wierd} itself is the
>>>>> {Wierd} that's being extended in the angle brackets the same {Wierd}
>>>>> that's being defined in this file?
>>
>> On a tangential note, it's best practice to make identifiers, such as your class name 'Wierd', either sufficiently obviously not intended to be a natural-language word that spelling is not an issue, or to spell it the same as the natural-language word it resembles.  So, for example, to model a boundary, 'bnd' would be an acceptable variable name, but 'boundray' would not be.  The reason is that it's too difficult for maintainers to get near-miss spelling correct - the pull to spell the variable or type name the same as the natural-language word is just too strong.
>>
>> So fix the spelling of 'Wierd'.
>>
> Weird, IntelliJ IDEA will not only auto-suggest the correctly misspelled 
> variant, but the latest version, with the Spell Correct plug in, will 
> offer to rename the symbol in question to the correct spelling, taking 
> into account Camel Case word boundaries and all.
> 
> Wow, talk about a run-on sentence. Oh well, the point is that tools are 
> cool and help us flawed humans recover from many common mistakes.

Tools can help one be more diligent; they cannot make one be diligent.

IntelliJ wouldn't have that feature were it not a best practice to spell identifiers intelligibly.

The programmer still has to be aware that there is such a best practice, and to follow it.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#8684

FromScreamin Lord Byron <scre@min.dot>
Date2011-10-10 19:07 +0200
Message-ID<f11dn5rfsqq6.s2k8wo2l2h9i$.dlg@40tude.net>
In reply to#8677
On Mon, 10 Oct 2011 08:01:34 -0700 (PDT), KevinSimonson wrote:

> I'm currently taking a class pretty much each week to prepare for the
> SCJP exam.  In the classes we usually go over practice exams and try
> to figure out what the right answer is to a number of the questions.
> After the chapter on Generics, one of the questions involved a class
> like the one I call {Wierd} here.  It was kind of of the form:
> 
> public class Wierd< T extends Wierd>
> {
>   public Wierd ()
>   {
>   }
> }
> 
> and then we had a class of the form:
> 
> public class Hmm
> {
>   public static void main ( String[] arguments)
>   {
>     Wierd< Wierd> wow = new Wierd< Wierd>();
>   }
> }
> 
> Can anyone tell me what's going on here?  In {Wierd} itself is the
> {Wierd} that's being extended in the angle brackets the same {Wierd}
> that's being defined in this file?  

It just means that the type parameter must fulfill an IS-A relation with
Wierd. It must be Wierd or any subclass of Wierd. That's it. Nothing is
being extended here.

> And what exactly is happening in
> {main()} when I declare {wow} to be a {Wierd< Wierd>} object?  I'd
> really appreciate it if someone could explain this to me.

Nothing special is happening. You just instantiate the object of a class
Wierd. From JVM's point of view it is exactly the same as:
Wierd wow = new Wierd();

[toc] | [prev] | [next] | [standalone]


#8695

FromJeff Higgins <jeff@invalid.invalid>
Date2011-10-11 02:08 -0400
Message-ID<j70mef$rkj$1@dont-email.me>
In reply to#8677
On 10/10/2011 11:01 AM, KevinSimonson wrote:

  also
  public class Foo< T extends Foo>
  {
    public Foo () {}

    public static void main ( String[] arguments)
    {
      Foo< Foo> bar = new Foo< Foo>();
    }

    /*
      Depending upon what you have between these curly braces
      mixing the raw type Foo with the parameterized type
      Foo<T extends Foo> might not produce fubar.
    */

  }

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web