Simple situation really. Just being a bit paranoid I guess.
I have a 2D grid full of objects which I need to represent in source.
The grid contains two objects, object 1 and object 2 which both are children of object 3 which is abstract.
Now the thing is I can either:
1. Create an ArrayList<ArrayList> list object. ArrayLists don't care what you store in them so this would work. However it's kind of a hack since I don't really need a re sizable list with all the shit and giggles.
2. Create a packer object. That stores both of objects 1 and object 2 without problems and store then Packer[][] grid = new Packer(); .
So what does /prog/'s wisdom have to say about this? 1, 2 or some new brilliant idea?
Do you not understand the point of inheritance in statically typed languages, or are you just bad at explaining problems?
Alternatively, use a real fucking language.
Name:
Anonymous2010-11-06 12:24
The problem is Java doesn't let an array store objects of a different type. Just in case the OP wasn't clear enough.
>>3
They both inherit from a common interface. Make that the array type. Fucking idiot.
Name:
Anonymous2010-11-06 12:28
class DifferentObject {
Object obj;
String type;
}
class Main {
public static void main(String[] args) {
DifferentObject[] array = new DifferentObject[10];
// add.. add.. add..
String type = array[i].getType();
if (type.equals("prog")) {
// do your thing here
}
}
}
What are you really suggesting to do? If I make an array of type object3 I don't think Java will let me store anything but object3. Also object3 is abstract and I'd really love to keep it that way. Perhaps typecasting object1/2 to object3? I don't know if that ill work?!
>>6 If I make an array of type object3 I don't think Java will let me store anything but object3.
You ``don't think'' it will? Have you even tried it? Learn the fucking language before you bother other people for help.
Also object3 is abstract and I'd really love to keep it that way.
I wish there were some emoticon that could communicate my reaction to this statement.
Rolf I'm actually surprised. Not a single post off topic. Way to go /prog/!
Anyway it was just a misunderstanding. I did the same thing some weeks ago and forgot to typecast and thought it was actually not legit to create an array of abstract objects. Anyway thanks!
Name:
Anonymous2010-11-06 16:56
Define an interface for creating an object, but let subclasses decide which class to instantiate then decouple the abstraction from its implementation allowing the two to vary independently without violating encapsulation make sure you capture and externalize the object's internal state allowing the object to be restored to this state later.