I have an ArrayList of type Object that contains 6 objects of different classes:
questions = new ArrayList<>();
questions.add(new Question1());
questions.add(new Question2());
questions.add(new Question3());
questions.add(new Question4());
questions.add(new Question5());
Depending on questionNum variable I have to get the object's method, i.e. isAnswered() from the ArrayList.
Example of what I want:
for (int i = 0; i < 5; i++)
{
questionNum = i;
if ((Question<questionNum>)questions.get(questionNum).isAnswered())
{
Log.d("Answered", String.valueOf(questionNum));
}
}
What I am using right now:
else if (questionNum == 4 &&
((Question5)questions.get(questionNum)).isAnswered())
Any way to make it more elegant or should I go with interfaces or inheritance (doing Android project, so the every question class already inherits Fragment)?