How to create a shortcut for a parameterized type

B

benoit.isaac

Hello all,

I am experiencing a problem trying to use the generics feature of Java
1.5.

Here it is:

- I have a Generic class Measure that is parameterized by any kind of
number:
public class Measure<U extends Number>{

private String name;

public Measure(String _name)
{
name = _name;
}

.....
}

- In my program, I want my measures to be either Float or Double, but I
would like to have to set this only in one place in the program.
So I would like to rename (ie, create a shortcut to) a Measure
parameterized. That is, I would like to do something like:

public class MeasureForce extends Measure<Double> {}

so that I can use everywhere : MeasureForce f = new MeasureForce()...
and if I want to switch to Float numbers, I just change the definition
of MeasureForce in one class file to :

public class MeasureForce extends Measure<Float> {}

Unfortunately, this doesn't seem to work. I can create the MeasureForce
class but then I cannot use the functions defined in Measure<U>, that
is, a call to : MeasureForce f = new MeasureForce("nameForce") gives me
an error saying that the "MeasureForce(String) constructor is not
defined".

How can I do what I would like to do ?

I also something like using Measure<UnitForce> where UnitForce would
extend Double, unfortunately it is not possible to extend Double nor
Float (final classes).

Thank you very much for any help on this,

Kind regards

Benoit
 
T

Thomas Weidenfeller

public class MeasureForce extends Measure<Float> {}

Unfortunately, this doesn't seem to work. I can create the MeasureForce
class but then I cannot use the functions defined in Measure<U>, that
is, a call to : MeasureForce f = new MeasureForce("nameForce") gives me
an error saying that the "MeasureForce(String) constructor is not
defined".

Implement the constructor. Constructors are not inherited.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top