0

I have an abstract class with several subclasses. A tester class has an ArrayList with 1 object of each subclass in it. They each have a method of the same name, how can I iterate through the ArrayList and call that method for each object?

One of my subclasses (others basically the same):

public class MyMath extends Homework {
private int pagesRead;
private String typeHomework;

public MyMath(int pagesRead, String typeHomework) {
    super(pagesRead, typeHomework);
}

public void createAssignment(int p) {
    setPagesRead(p);
    setTypeHomework(typeHomework);
}

public void toString(int pagesRead, String typeHomework) {
    System.out.println("Homework type: " + typeHomework + ". Number of pages read: " + pagesRead + ".");
}

}

In my tester class main method:

ArrayList homework = new ArrayList();
homework.add(new MyMath(5, "Math"));
homework.add(new MyScience(5, "Science"));
homework.add(new MyEnglish(5, "English"));
homework.add(new MyJava(5, "Java"));
Stephanie Fu
  • 79
  • 1
  • 9

4 Answers4

1

Well, if the method is specified in your abstract class, and you have already built the ArrayList with all the objects inside it, you should simply be able to iterate through the ArrayList (for-loop) and just call the .method()

Michael
  • 776
  • 4
  • 19
  • Can I use a foreach loop, or does that not work on this because they are different types of objects? – Stephanie Fu Feb 23 '16 at 04:44
  • The for-each construct should work fine. – markspace Feb 23 '16 at 04:46
  • Yes, as markspace has said, the for-each construct should work! I was just giving an example of a way to iterate through the ArrayList – Michael Feb 23 '16 at 04:47
  • @StephanieFu As long as the objects have already been initialized, I should think you could use a for each such as `for(SuperClass a : list)` and in the loop, `a.method();`. – Calvin P. Feb 23 '16 at 04:48
  • @CalvinP. I tried doing that, and I got a "Type mismatch: cannot convert from element type Object to Homework" error. Homework is the superclass. – Stephanie Fu Feb 23 '16 at 04:51
  • Thanks for the answers, I figured what I was doing wrong - I didn't create the ArrayList correctly, so the types were all Objects instead of Homeworks. – Stephanie Fu Feb 23 '16 at 04:57
0

If your ArrayList is of type and your interface also has the method then you can call them like so

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

You can iterate through the arraylist ant it contains objects and you can call the method on that object. for example,

ArrayList<AbstractClass1> objs = new ArrayList<AbstractClass1>();

objs.add(); // you have added objects already.

Then

for(int i = 0; i< objs.size() ; i++){

objs.get(i).methodYouDefined();

}
0

If you haven't covered generics yet in class, you have to cast manually. If you have covered generics, you should use them! See lak91's answer.

public abstract class AbstractTest
{
   public abstract void oneTwo( int i, String s );

   public static void main(String[] args) {
      List list = new ArrayList();
      list.add( new One() );
      list.add( new Two() );
      for( Object test : list ) {
         AbstractTest abTest = (AbstractTest) test;
         abTest.oneTwo( 0, "test" );
      }
   }

}

class One extends AbstractTest {

   @Override
   public void oneTwo( int i, String s )
   {
      System.out.println("One");
   }

}

class Two extends AbstractTest {

   @Override
   public void oneTwo( int i, String s )
   {
      System.out.println("Two");
   }

}
markspace
  • 10,621
  • 3
  • 25
  • 39