Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-07-2008, 11:20 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Object array query
I have an object array called objectArray:

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-07-2008, 12:30 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
It's simple forward thing if you only want TagIt then do something like this ..
Code:
TagIt tagItArray = new TagIt[5] ;
There is nothing Generics associated with it.
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-07-2008, 02:49 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-07-2008, 03:50 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-07-2008, 04:43 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-07-2008, 07:58 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Tim is right,

Other option will be using ArrayList + Generics
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with creating array of type object riz618 New To Java 3 01-29-2008 08:14 AM
Using sql:query tag Java Tip Java Tips 0 01-15-2008 05:13 PM
how to remove an object from the array list cecily New To Java 3 08-02-2007 04:26 PM
Creating object of Type Object class venkatv New To Java 3 07-17-2007 05:33 PM
Operator < cannot be applied to java.lang.Object, Object Albert Advanced Java 1 07-13-2007 05:19 PM


All times are GMT +3. The time now is 02:39 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org