Is there anyway of doing this?

Im trying to have an array of classes and each of them has a function named run which acts differently for each class. i want them to run their version of the run function in the order they were added to the array. heres a simplified version that im trying to get working first before i write out the whole program.

class test

{

publicstaticvoid main(String[] args){

test2 tt =new test2;

}

}

class test2

{

private test2[] chap =new test2[3];

publicvoid test2(){

for(int i = 0; i<=2 ; i++)

{

chap[i] =new stuff();

}

for(int i = 0; i<=2 ; i++)

{

chap[i].run();

}

}

class stuffextends test2

{

publicvoid run()

{

System.out.println("Hello World!");

}

}

}

Sorry about the indentation.

This code gives the error

test2.java:11: cannot find symbol

symbol: method run()

location:class test2

chap[i].run();

1 error

thanks in advance,

Spoonly.

[2178 byte] By [Spoonlya] at [2007-10-2 6:38:22]
# 1
You are on the right track. But your run() method in the "stuff" class doesn't override the run() method in the "test2" class because there isn't one. So just put a run() method into test2. It doesn't have to do anything.
DrClapa at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...
# 2

oh right thanks a million.

now its compiling but when i run it nothing happens. it just runs and finishes and theres no output. anything else i should be doing? do i have to specify that i want it to override the original run function or something like that?

Thanks again,

Spoonly

Spoonlya at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...
# 3
What does your run() in test2 look like?
MLRona at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...
# 4
its just an empty functionpublic void run(){}
Spoonlya at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...
# 5
Did you recompile ALL files after you added "run" to test2?
MLRona at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...
# 6

yeah. why? should it be working now?

the only change ive made from the code above is that i added

public void run(){}

to test2.

then i compiled it all and ran it and theres no errors but nothing gets put out.

thanks for all the help so far.

Spoonlya at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...
# 7

You are welcome.

I just noticed the problem.

It doesn't work because this:

public void test2() { ... }

is NOT a constructor. So, Java makes its own default no-args do-nothing constructor.

Try taking out the word "void" to change that to a constructor. That might give you recursion problems. If so, put back void and change the name to something else:

public void runAll() { ... }

Then, it test main, do this:

test2 tt = new test2();

tt.runAll();

MLRona at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...
# 8
ah ha! yes, that worked.i had to change the name to something else then run it from main.Thanks a million,Spoonly.
Spoonlya at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...
# 9
Great! Glad I could help.
MLRona at 2007-7-16 13:46:27 > top of Java-index,Java Essentials,New To Java...