• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

How do I create a generic "type" in java?

I want to create a container class in Java.  I am very new to java but have experience in C++.  In C++ I could introduce the notion of a template type.  Essentially what I want to do is create a container class that will allow a user of the class to create a "stack of plates" or a "stack of integers" or a "stack of objects" or whatever.  I am not actually creating a stack - So please do not hand me a stack class.  What I am trying to figure out is how to create a container classes generic functionality in a similar maner as C++ does.  

Thank you Experts,

-TH
0
travishaberman
Asked:
travishaberman
  • 2
1 Solution
 
InteractiveMindCommented:
Hi TH,

could you please provide an example in C++, perhaps, of what you're trying to achieve exactly? I'm not completely clear.

Regards;
0
 
suprapto45Commented:
Hi,

I am not really understand on the question. Can you clarify more?

Best Regards
Dave
0
 
InteractiveMindCommented:
travishaberman,

after re-reading your question, I think that a Hashtable/Vector may be useful to you?
0
 
mattchssmCommented:
In C++ you can specify templated generic classes in this way: stack<plates>, stack<int>, stack<float>, etc.

Previously, Java only had a polymorphic-based solution. Java has a series of standard "Collection" classes, such as the "LinkedList" class.  Every item stored in one of these "Collection" objects is an "Object". "Object" is the parent class that all other classes inherit from, and therefore can be type-cast to any class that you want. For example, the following code shows how to add, and retrieve, an integer to a "LinkedList" object.

 List myIntList = new LinkedList();  
 myIntList.add(new Integer(0));
 Integer x = (Integer) myIntList.iterator().next();

But now Java 5.0 has "generics". It looks similar to C++, this is the typical usage:

 List<Integer> myIntList = new LinkedList<Integer>();
 myIntList.add(new Integer(0));
 Integer x = myIntList.iterator().next();

And you specify your own classes like this:

 class LinkedList<E> implements List<E> {
   // ... implementation ...  
 }

Read http://java.sun.com/developer/technicalArticles/J2SE/generics/ for detailed explanations of how to define your own "generic" classes and methods. Note that Java "generics" are actually very different in concept to C++ templated classes, so you should also read all the background information too.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now