Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By Fubarable
  • 2 Post By Tolls

Thread: Multiple object instances in array of objects

  1. #1
    Drizzt is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Multiple object instances in array of objects

    What i have is 3 classes, C1, the parent class and C2,C3 are the classes that extend C1. I'm also creating an object array in the main method like this C1 table[]=new C1[3]; and i'm inserting objects from all these 3 types. For example i want to add in the first cell a C1 type object , a C2 type in the second and a C3 type in the third cell. I've done this in the main method like this: table[0]=new C1(1) {}; table[1]=new C2(2,3); table[2]=new C3(3,4); The problem is that,despite the program being able to compile, only the 'a' variable of the C2 and C3 objects are stored in the array, but i want to store all their data in this array. Below are these 3 classes.

    public abstract class C1 {
    protected int a;
    protected C1(int a){
    this.a=a;
    }
    public void setA(int a){
    this.a=a;
    }
    public int getA(){
    return this.a;
    }
    }
    ----------------------------
    public class C2 extends C1{
    protected int b;
    protected C2(int a,int b){
    super(a);
    this.b=b;
    }
    public void setB(int b){
    this.b=b;
    }
    public int getB(){
    return this.b;
    }
    }
    ---------------------------
    public class C3 extends C1{
    public int c;
    protected C3(int a,int c){
    super(a);
    this.c=c;
    }
    public void setC(int c){
    this.c=c;

    }
    public int getC(){
    return this.c;
    }
    }

    What do i have to do to make this work? Maybe any alternative ways? Thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Multiple object instances in array of objects

    The other data is in fact being stored. Your problem is that you can't call the getB() or getC() methods on a C1 variable without first casting it, and you don't want to cast it without first testing what type of object is held by the variable -- a dangerous design for a program by the way.
    quad64bit likes this.

  3. #3
    Drizzt is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Multiple object instances in array of objects

    You mean what type is held by each cell of the array? Also this might seem a bit silly to ask but how can i cast it so that i can gain access to those methods?

  4. #4
    Drizzt is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Multiple object instances in array of objects

    Never mind the casting i just got that right. Thanks for the help so far but I'd still like an example of a more 'secure' solution if you could give me

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Multiple object instances in array of objects

    Why define a class 'abstract' if you're just going to try and instantiate it anyway?

    But Fubarable says, it's often (not always) a red flag when you find yourself casting the contents of a collection in order to do some work. It tends to imply the collection is too loose, or simply being used as a place to put things.

    In your case if you are using a fixed array with always one of each of these things, then it's likely they should simply be separate attributes of a class.
    DarrylBurke and Fubarable like this.

  6. #6
    Drizzt is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Multiple object instances in array of objects

    This is the logic behind a project i'm making in the storing data part,where the class needs to be abstract,which are hundreds, this was just an example. Thank you anyway

  7. #7
    chimerax is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: Multiple object instances in array of objects

    Quote Originally Posted by Drizzt View Post
    Never mind the casting i just got that right. Thanks for the help so far but I'd still like an example of a more 'secure' solution if you could give me
    Any chance i could take a look at your finished main code? I have the same issue but I'm struggling with the casting part.

Similar Threads

  1. Multiplayer game - multiple instances
    By robs in forum Java Gaming
    Replies: 0
    Last Post: 04-18-2011, 09:33 AM
  2. Multiple GUI Interface Instances
    By mutagen in forum AWT / Swing
    Replies: 2
    Last Post: 03-19-2011, 03:06 PM
  3. Replies: 16
    Last Post: 05-22-2010, 03:51 AM
  4. Replies: 1
    Last Post: 05-04-2010, 11:00 AM
  5. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 03:57 AM

Tags for this Thread

Posting Permissions

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