Results 1 to 7 of 7
- 01-11-2012, 04:10 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
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
-
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.
- 01-11-2012, 06:01 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
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?
- 01-11-2012, 07:00 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
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
- 01-11-2012, 10:11 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 01-12-2012, 12:08 AM #6
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
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
- 01-12-2012, 04:07 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Multiplayer game - multiple instances
By robs in forum Java GamingReplies: 0Last Post: 04-18-2011, 09:33 AM -
Multiple GUI Interface Instances
By mutagen in forum AWT / SwingReplies: 2Last Post: 03-19-2011, 03:06 PM -
Drawing multiple instances of one class with an array
By Grimmjow in forum New To JavaReplies: 16Last Post: 05-22-2010, 03:51 AM -
adding or removing an object from an array of objects
By javanew in forum New To JavaReplies: 1Last Post: 05-04-2010, 11:00 AM -
Can I store multiple objects in an array
By lareauk in forum New To JavaReplies: 9Last Post: 05-29-2008, 03:57 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks