Question

you have

public class Question

and

public class MultipleChoice extends Question

and

public class SurveyQuestions

where SurveyQuestions has

private static List<Question> q = new ArrayList<Question>();

In main you keep adding questions and all the things questions are composed of to the list.

Once done, you want to iterate over the list

for (Question q : SurveyQuestions.getInstance().getListOfQuestions())

Question: What type should "q" be so that it has access to both Question and MultipleChoice?

Was it helpful?

Solution

If by "access to" you mean "access to the methods of"- you can't have it as MultipleChoice because some questions might not be such. So it has to be Question, and then you can check if it is a multiple-choice by if(q instanceof MultipleChoice)

OTHER TIPS

You should make it Question.

You can't make it MultipleChoice as a plain Question is not a MultipleChoice... but a MultipleChoice is a Question. So Question can represent both types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top