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"));
6
  • Does the abstract class also have a method with the same name (and parameter types)? Commented Feb 23, 2016 at 4:41
  • @Stephanie , adding some of your code would be helpful! Commented Feb 23, 2016 at 4:43
  • The abstract class has an abstract method that each subclass overrides. Commented Feb 23, 2016 at 4:45
  • Then any list iteration (for-each, plain for loop, etc.) should work fine, as given in both answers below. Commented Feb 23, 2016 at 4:47
  • 1
    Use List<Homework> homework = new ArrayList<>(); Commented Feb 23, 2016 at 4:53

4 Answers 4

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()

Sign up to request clarification or add additional context in comments.

6 Comments

Can I use a foreach loop, or does that not work on this because they are different types of objects?
The for-each construct should work fine.
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
@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();.
@CalvinP. I tried doing that, and I got a "Type mismatch: cannot convert from element type Object to Homework" error. Homework is the superclass.
|
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();
}

Comments

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();

}

Comments

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");
   }

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.