File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Identifiers and the <> notation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Identifiers and the <> notation" Watch "Identifiers and the <> notation" New topic
Author

Identifiers and the <> notation

Jeremy Watts
Greenhorn

Joined: Jun 23, 2011
Posts: 18
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

Joined: May 29, 2005
Posts: 479
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Jun 23, 2011
Posts: 18
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Oct 27, 2005
Posts: 19685
    
  20

Actually subtype, not subclass. The difference is subtle, but a class implementing an interface is a subtype of that interface, not a subclass.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Yes, you are right about subtype, Rob.
 
 
subject: Identifiers and the <> notation