Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

How to create a shortcut for a parameterized type

Thread view: 
benoit.isaac@gmail.com - 15 Feb 2006 10:13 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.