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

Posted on 2005-04-16
Last Modified: 2010-04-17
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,

Question by:travishaberman
    LVL 25

    Expert Comment

    Hi TH,

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

    LVL 16

    Expert Comment


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

    Best Regards
    LVL 25

    Expert Comment


    after re-reading your question, I think that a Hashtable/Vector may be useful to you?
    LVL 2

    Accepted Solution

    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 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.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now