We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Array of child objects or different objects
Page Index Toggle Pages: 1
Array of child objects or different objects (Read 602 times)
Array of child objects or different objects
Dec 18th, 2008, 5:28pm
 
Hi,

it's seems that java don't consider child objects ( extend class ) as the same type of objects ( referring to the parent class ).

So if you do:

ParentObj[] myObjects = new ParentObj[2];

myObjects[0] = new childOne();
myObjects[1] = new childOne();

for(int i=0; i<myObjects.length; i++){
myObjects[0].update();
}

The program will run except if update() is a function that only exist in the extended class ( child ). Which is not good.

So We found two solutions, that I am going to test:

First one by doing a ArrayList, which means a array of Object Array. Probably will work but feels a bit weird to create arrays with one object.
ArrayList[ ChildOne[OneObject], ChildTwo[OneObject], ect...]

Second solution is to do like in this example ( http://www.allapplabs.com/java_design_patterns/factory_pattern.htm ). You have a Array of String where the Strings are the Class Names, and then make a function that says: "For that name create a Object of that type". But that means you need to update the function every time you create a new child.

If anyone has any thoughts on that or already know how to sort this thing. I will post a example of the solution that I found best.

Re: Array of child objects or different objects
Reply #1 - Dec 18th, 2008, 6:05pm
 
(ah, I guess I needed to read the question more carefully)
Re: Array of child objects or different objects
Reply #2 - Dec 18th, 2008, 8:16pm
 
Perhaps:
Code:
void setup()
{
ParentObj[] myObjects = new ParentObj[2];

myObjects[0] = new ChildOne();
myObjects[1] = new ChildTwo();

for (int i = 0; i < myObjects.length; i++)
{
if (myObjects[i] instanceof ChildOne)
{
((ChildOne) myObjects[i]).update();
}
else if (myObjects[i] instanceof ChildTwo)
{
((ChildTwo) myObjects[i]).downdate();
}
myObjects[i].doh();
}
}

class ParentObj
{
String foo;
ParentObj() { foo = " > "; }
void doh() { println(foo + "Doh!"); }
}
class ChildOne extends ParentObj
{
void update() { foo = " <> "; }
}
class ChildTwo extends ParentObj
{
void downdate() { foo = " >< "; }
}
Re: Array of child objects or different objects
Reply #3 - Dec 19th, 2008, 11:10am
 
Hi, thanks for the reply. Didn't get sw01 comment.
Anyway, I just tried your solution. Works, but I don't see the point of using a loop as you have to make a statement every time i is incremented. You could just write:

((ChildOne) myObjects[0]).update();
((ChildTwo) myObjects[1]).update();


Even more if your are not doing a loop, what's the point of doing a array? let's just write:

tom = new ChildOne();
bob = new ChildTwo();

tom.update();
bob.update();

See what I mean?


//Modification:
But using a array is finally better as you don't have to name your childs ( tom, bob, etc...). Smiley
Re: Array of child objects or different objects
Reply #4 - Dec 19th, 2008, 11:36am
 
And it's the same problem with ArrayList and HashMap, Because you have to define the type of Child for each Child and/or doing statement.

To bad is not as easy as in php where array can have a mix type of objects.

A Array of ArrayList would be efficient only if you have multiple instance of every child.

int nbrOfChildType = 4;
int nbrOfBts = 10;
ArrayList[] parentObj = new ArrayList[nbrOfChildType];

void setupChilds(){
 for(int i=0; i<parentObj.length; i++){
   parentObj[i] = new ArrayList();
   for(int j=0; j<nbrOfBts; j++){
     if(i==0) parentObj[i].add ( new ChildOne() );
     if(i==1) parentObj[i].add ( new ChildTwo() );
   }

 }

}

void drawChilds(){
 for(int i=0; i<parentObj.length; i++){
   for(int j=0; j<nbrOfBts; j++){
      if(i==0) ((ChildOne) parentObj[i].get(j)).update();
      if(i==1) ((ChildTwo) parentObj[i].get(j)).update();
   }
   
 }
}

Which means:

parentObj [ [ChildOne,ChildOne,ChildOne,etc...], [ChildTwo,ChildTwo,ChildTwo,etc...] ]
Re: Array of child objects or different objects
Reply #5 - Dec 19th, 2008, 12:49pm
 
Perhaps you can use interface instead of inheritance to specify the appropriate subset of array, e.g,

Code:

interface Updatable{
 void update();
}

class ParentObj{
ArrayList childobj;
// this can be ArrayList<ParentObj> if you can use generics.
}

class ChildOne extends ParentObj implements Updatable{
 void update(){ println("foo");}
}

class ChildOne extends ParentObj implements Updatable{
 void update(){ println("bar");}
}



and use (obj instanceof Updatable) to evaluate its type.


...wait, reading your code, I'm not sure whether you are trying to use the parent and child in the context of inheritance or some kind of tree-shaped data structure.  This is because the OP talks about the former, but the last post talks about the latter.  Could you clarify?
Re: Array of child objects or different objects
Reply #6 - Dec 19th, 2008, 3:24pm
 
Thanks sw01 for searching.

My Child's object are just the same as a Parent object but with extra things. Extra things that are not always the same for every Child.

All inheritance works, I am looking for a way to manage those child objects in a array. I don't think a interface is appropriated. But maybe I don't see the full picture.
Re: Array of child objects or different objects
Reply #7 - Dec 19th, 2008, 4:14pm
 
You're welcome.  But, uh...."searching"?  I'm assuming something got lost in translation.

Anyway, why do you think using an interface is not appropriate?  Please clarify.  Perhaps a case in Java or even PHP might help illustrate your problem.


I think PhiLho's solution answers your question already, and he used different methods on each type of chldren to show an extreme case.  The point was that instanceof operator obviates any elaborate scheme for type check.  As you have mentioned, overriding .update() is the preferred solution, but the problem in OP was that Parent class does not necessarily have an .update() method.

I suppose use of interface is easier demonstrated than explained. It's a common practice - I'm not demonstrating anything exotic.

Note how instanceof operator is used, like PhiLho's case, but it is used to detect an interface instead of a specific class.

I've also randomized the array to show a case where handcoding a series of object instantiation is not desirable.  It also throws the parent class in the mix to show that it does not choke the mass .update() process.

Furthermore, iterator is used to drive home the point that the container is not the problem here, as array, ArrayList, and HashMap all can contain any type of objects.

By the way, you absolutely don't want to use your code where you assign an arbitrary index number to a certain class and assume it to always be the case - that's really asking for trouble further down the road. Keep it simple!

Code:

import java.util.ArrayList;
import java.util.Iterator;
ArrayList listOfObjects;

interface Updatable{
 void update();
}

class ParentObj{
 ArrayList childobj;
 // this can be ArrayList<ParentObj> if you can use generics.
}

class ChildOne extends ParentObj implements Updatable{
 void update(){ println("foo");}
}

class ChildTwo extends ParentObj implements Updatable{
 void update(){ println("Doh");}
}



void setup()
{
 listOfObjects = new ArrayList();
 
 for(int i = 0; i < 20; i++){
   float r =  random(1);
     //println(r);
     if(r < 0.2){
       listOfObjects.add(new ChildOne());
     }else if(r < 0.8){
       listOfObjects.add(new ChildTwo());
     }else{
       listOfObjects.add(new ParentObj());
     }
 }
 Iterator  itr = listOfObjects.iterator();
 while(itr.hasNext()){
   Object obj = itr.next();
   if (obj instanceof Updatable){
      ((Updatable)obj).update();
   }
 }
   
}
Re: Array of child objects or different objects
Reply #8 - Dec 19th, 2008, 6:24pm
 
Either your children have something in common - in which inherit/override (if you can modify Parent) or implement an interface to expose it (if you can't modify Parent) as per sw01's post

(note that if you only put Updatable objects in that ArrayList then it could have alternatively have been declared as an array of Updatable, similar to your first post, avoiding the need to use instanceof and typecasting)

..or they don't - in which case i'd agree with the earlier comment questioning the benefit of storing/accessing them all from the same container (whether array, arraylist, etc)

maybe you need to give a more complete example of your classes and how they're used them if nothing so far sounds like it solves the problem?

hth
Page Index Toggle Pages: 1