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
Thomas Weidenfeller - 15 Feb 2006 11:16 GMT
> public class MeasureForce extends Measure<Float> {}
>
[quoted text clipped - 3 lines]
> an error saying that the "MeasureForce(String) constructor is not
> defined".
Implement the constructor. Constructors are not inherited.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Roedy Green - 15 Feb 2006 12:01 GMT
>that
>is, a call to : MeasureForce f = new MeasureForce("nameForce") gives me
>an error saying that the "MeasureForce(String) constructor is not
>defined".
this has nothing to do with generics. You must redefine all the
constructors you need with every extending.
See http://mindprod.com/jgloss/constructor.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.