Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

templated classes

Status
Not open for further replies.

mackey333

Technical User
Joined
May 10, 2001
Messages
563
Location
US
Is there a way to template classes in java (make them work with more than one data type). I've tried using Object and Number for the variable inside the class. This works limitedly with data going into the class, but requires an explicit cast after returning variables. I am trying to create a Vector class that will work with all data types.

-Greg :-Q

flaga.gif
 
Doesn't Vector already do that? You put an Object in, you get an Object out.

I can understand your wanting to avoid casting things on the way out. I don't think it's possible, though.
 
I haven't used java's built in Vector class. I am trying to create my own as a learning experince...I have all the code to work but so far only in seperate classes: IntVector, StringVector, etc..

-Greg :-Q

flaga.gif
 
I'd just write it using an Object receive and return type since if you're using the Vector you will know the data type and hence no real problem casting. If you wish to create a related Vector, use interfaces for the classes and write the methods to take interface types. Like a RNumber interface for Float and Double. Things like that. Hope that helps, JavaDude32.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top