Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating classes with generic objects

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating classes with generic objects

    Hi,

    I want to create a data structure class which works for more than one type of object, but each instance of the data structure should hold only one type of data specified when it is instantiated. For example I could have a queue of apples or a queue of oranges, but not a queue of apples and oranges. I'm fairly certain I need to use a generic object to do this but I'm not sure how. Any ideas?

    Thanks


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Creating classes with generic objects

    Yes, you want "generics" - the term used, rather than "generic object. There is a good description in its own chapter Oracle's Tutorial.

    This is (from a Java perspective) such a natural way of dealing with data structures that you find generics used pretty much whereever collections of any sort are used. Before writing your own class make sure you are happy with how generics are used with, eg, ArrayList or even java.util.Queue. Being familiar with generics as a "user" will help when it comes to implementing classes this way as an "author".

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    rbt (April 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating classes with generic objects

    Hi,
    Thanks so much for the link. I am familiar with using generics: I have used ArrayList and Comparable and other classes that use them, I was just unsure how to implement them syntactically.

Similar Threads

  1. Question about creating and using classes.
    By iDizzle in forum Object Oriented Programming
    Replies: 20
    Last Post: April 1st, 2012, 03:14 PM
  2. Creating random objects
    By JackCannon15 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 18th, 2011, 08:50 AM
  3. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM
  4. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  5. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM