0

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

2
  • Do your different questions differ in functionality? Commented Apr 28, 2015 at 21:53
  • You should use interface and delegate the functionality to the appropriate class Commented Apr 28, 2015 at 21:57

2 Answers 2

1

You can have each Question class implement an interface:

public interface Question{
    public boolean isAnswered(); 
}

public class Question1 implements Question{
    public boolean isAnswered(){
        //implementation
    }
    //other code
}

Then add these as Questions to the List

List<Question> questions = new ArrayList<Question>();
questions.add(new Question1());

Then you can get the Question from the List and call the appropriate method:

if ( questions.get(0).isAnswered() ){

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

3 Comments

You have to do casting to (Question) interface to make it correct isAnswered() call. I do get Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference when I do the if statement. Any hints on avoiding it?
Casting shouldn't be necessary if the List is parametized to the class Question. java.lang.Object java.util.List.get(int)' on a null object reference is the List null?
My bad, I had the list declared twice: as a field and in the code. Changed the type in the field and it works like a charm!
0

i see three solutions here:

A. Add a typecaster along with each question on ArrayList like this:

questions = new ArrayList<>();
questions.add(new ArrayList<>(Arrays.asList(new Question1(), "Question1")));
questions.add(new ArrayList<>(Arrays.asList(new Question2(), "Question2")));

Then have a switch statement depending on the second type argument and type cast (literally) as needed

B. Have each Question* class have a property type and then use a switch statement (as previously) to typecast appropriately, i.e:

class Question1
{
    public String type = "Question1";
    // ...
}
// ...

C. Have all Question* classes inherit from common Question superclass or implement common interface as needed, i.e:

class Question
{
    public String type = "Question";
    public Boolean isAnswered()
    {
        return false;
    }
    // ...
}

class Question1 extends Question
{
    public String type = "Question1";
    public Boolean isAnswered()
    {
        // ..
    }
    // ...
}
// ..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.