Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What Does it Mean to Pass a Generic Class Itself as a Parameter?

Reply
Thread Tools

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

 
 
KevinSimonson
Guest
Posts: n/a
 
      10-10-2011
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
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      10-10-2011
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>
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      10-10-2011
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.


 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-10-2011
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>
 
Reply With Quote
 
Screamin Lord Byron
Guest
Posts: n/a
 
      10-10-2011
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();
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-10-2011
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 asthe 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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      10-10-2011
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.
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-10-2011
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?
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-10-2011
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 sameas 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
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-11-2011
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.
*/

}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery Jeff Kish XML 4 10-30-2008 05:47 PM
What does class keyword in a Class mean? Hongyu C++ 2 08-22-2008 01:55 PM
What does it mean to pass a function name? Steven T. Hatton C++ 2 12-09-2006 02:51 AM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
What does 'You should pass XmlResolver to Transform() method' mean??? Showjumper ASP .Net 0 07-03-2003 01:18 PM



Advertisments