Hi guys!

I found a lot of my answers on this webbie, so I registered myself.

I'm creating a java based quiz program.

Now i'm running into a lot of problems trying to get stuff working.

The problem i'm having now is the following:

I have a class Question, and 2 sub classes WrongCorrectQuestion, and MultipleChoiceQuestion.

Now, the class Question has String question, as attribute.
The class WrongCorrectQuestion has String answer as attribute. And getters and setters.

Now, in my (in the database class) method fillArray(), which should fill an array with objects from one of the two sub classes.

Now i try to do the following:
outputArray[counter] = new WrongCorrectQuestion();
but I can only use the methods from the superclass Question. Eventho i instantiate it as a WrongCorrectQuestion object.

I think i'm missing some theory about this. Anyone could help me out? Thnx!

Ps: I could upload the entire program, but i'm belgian, and everything is in dutch, like var names and class names.

Thanks in advance guys!!

6 Years
Discussion Span
Last Post by JamesCherrill

What's the type of array `outputArray`? If it's a Question array, each reference would be of type Question which kinda explains everything.


Well yes. I'm with you that far. But the procedure goes something like this:

From class GUI, i call fillArray(), which is located in my class DatabaseCon. There the method fills an array with 10 random questions from random type, and then returns that array to gui. Where it will go through the array, and then print. So, basicly, i need something to store X number of the two sub classes. And then return that variable that contains those subclass objects.

I could make the methods i need to access for each subclass abstract in the super, but that doesnt work for all methods.

Do you get my drift?

Thnx for the reply !


You could use instanceOf to see which subclass each Question is, then cast the Question to that subclass. after which you can access the subclass methods

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.