• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Identifiers and the <> notation

 
Jeremy Watts
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What are the set of '<>' called, containing an identifier, that you sometimes see after class definitions? Example:-

abstract class X<T> {
public abstract T yourMethod();
}

class X1 extends X<X1>
{
public X1 yourMethod()
{ return this;
}
}

I cannot seem to find reference to the use of these things anywhere. How are they used? In what circumstances? What do they do??
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Watts wrote:Hi,

What are the set of '<>' called, containing an identifier, that you sometimes see after class definitions? Example:-

abstract class X<T> {
public abstract T yourMethod();
}

class X1 extends X<X1>
{
public X1 yourMethod()
{ return this;
}
}

I cannot seem to find reference to the use of these things anywhere. How are they used? In what circumstances? What do they do??


BIG topic. Try http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

rc
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The references are in Effective Java™ by Joshua Bloch. Unfortunately I can't find my copy because all our furniture is in the wrong place while we have builders in, so I can't give you the page number..

List<T> The "T" is a formal type parameter..
List<String> The "String" is an actual type parameter.
 
Jeremy Watts
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both. I've been trying to use these to accomplish some form of 'covariant return' typing, with little success.
I have written an abstract class, called 'Field' , defined as:-

public abstract class Field
{
abstract double getValue();
}

public class Real extends Field
{
public double getValue()
{
return(2.0);
}
}

Now for another class, 'Complex', that also extends field, then it's implementation of 'getValue()', I want
to have a different return type for, other than 'double' (either 'void' or an error type). How could this be acheived?
So maybe the class would look like:-

public class Complex extends Field
{
public void getValue()
{
}
}


Thanks
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually (based on my 30 seconds of googling) the "covariant" return type is supposed to be related in some way to the base return type, and usually it's supposed to be a subclass of the base return type.

That doesn't seem to be the case in your example, although since you haven't voted for an actual type to represent a Complex object it's hard to tell.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a covariant return type (available since Java5) is a subclass of the superclass' method's return type. That has to do with overriding and polymorphism, not with the <T> notation at all.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually subtype, not subclass. The difference is subtle, but a class implementing an interface is a subtype of that interface, not a subclass.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right about subtype, Rob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic