I have some struggle with an exercise. I want to save question1 and question2 in the method addQuestion. But I have no idea how to access f and add it into something that is accessible. In my schoolbook they told us that we should do it with polymorphism. When I'm starting my debugger I see that when q.addQuestion is called, there is a property f with the fields that I need. But how can I save them for later? My first though was I can do it with ArrayList but I can't use any Imports (it's a rule in the schoolbook).
Probably I don't understand polymorphism correctly.
public class something{
public static void main(String[] args) {
Quiz q = new Quiz(2);
q.addQuestion(new question1("SomeText",5,157,5));
q.addQuestion(new question2("SomeText",5,157,5,123));
...
}
}
public class Quiz {
int myNumQuestions;
Quiz[] quizQuestion;
Quiz(int numQuestions){
this.myNumQuestions = numQuestions;
quizQuestion = new Quiz[myNumQuestions];
}
public boolean addQuestion(Interface f){
quizQuestion = new f(); //that does not work but how can I access the field from f?
}
Regards.
EDIT:
I changed my public class Quiz a little bit and I can now allocate the f
MyInterfaceName[] = quizQuestion;
Quiz(int numQuestions){
this.myNumQuestions = numQuestions;
quizQuestion = new Quiz[myNumQuestions];
}
public boolean addQuestion(Interface f){
quizQuestion[mycounter++] = f;
}