T-Dog18 T-Dog18 - 3 months ago 12
Java Question

initialise superclass array in subclass (java)

I am new to JAVA / Android programming and have a small problem.

I created in a Superclass a Array and wanted to initialize it in multiple subclasses. But when I try to initialize it, it says it's not possible.

My code:

public abstract class Fragen {

String[] Deutsch;
String[] Slowakisch;

static int Anzahl;
Random random;
int randNumber;

byte Fächer;

public String displayQuestion()
{

//TODO Fach abfragen
randNumber = random.nextInt(Anzahl);

return Slowakisch[randNumber];

}

public boolean correctAnswer(String answer)
{
//TODO Fächer +/-

if(answer.equals(Deutsch[randNumber]))
return true;

else
return false;
}
}


(Superclass)

public class Lektion1 extends Fragen
{
private Lektion1()
{
super();
Anzahl = 60;
//Deutsch = new String[];
Deutsch = {"",""};
Slowakisch = {"",""};
}
}


(Subclass)

Kind regards
Thomas

Answer

Try this:

public class Lektion1 extends Fragen
{
    private Lektion1()
    {
        super();
        Anzahl = 60;
        //Deutsch = new String[];
        Deutsch = new String[]{"", ""};
        Slowakisch = new String[]{"", ""};
    }
}