Results 1 to 6 of 6
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Object array query

    I have an object array called objectArray:

    Java Code:
    Object[] objectArray = new Object[5];
    I want it to only store objects of child classes of my custom class "TagIt".

    Is this possible to do this using Generics?

    Thanks for your time.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    It's simple forward thing if you only want TagIt then do something like this ..
    Java Code:
    TagIt tagItArray = new TagIt[5] ;
    There is nothing Generics associated with it.
    dont worry newbie, we got you covered.

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks but I want it to only store objects of child classes of my custom class "TagIt". Which means:

    TAGIt has 3 classes that inherit from TAGIt. I want to store the objects of these classes in the array.

  4. #4
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    The answer that Roots gave will do exactly that for you. The declaration they gave means that only objects of type TagIt or any of its subclasses will be allowed to be put in the array. Attempts to put any other type in it will fail.

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Abstract class

    Hello bugger.

    If you make TagIt an abstract class, then you cannot put an instance of that class into your array - only its non-abstract sub classes.
    Quote Originally Posted by bugger
    Thanks but I want it to only store objects of child classes of my custom class "TagIt".
    It does the same as what jelly and roots suggested, but it will exclude TagIt.

    Hope this helped. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  6. #6
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Tim is right,

    Other option will be using ArrayList + Generics
    Java Code:
    		ArrayList<? extends TagIt> apple = new ArrayList<TagIt>();
    		apple.add(new TagIt()); // Fails as only subclasses are accepted
    dont worry newbie, we got you covered.

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 07:14 AM
  3. Using sql:query tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 04:13 PM
  4. how to remove an object from the array list
    By cecily in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:26 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •