car
car

Reputation: 35

Compiler Error for method

I'm having trouble with a little program I'm making. Basically I have 6 Classes. 1 main class, 4 sub classes that "extend" the main class and another class to run the program. The class to run the program is laid out this so far:

public class ClassToRunProgram {

public void main(String[] args){

Class1 a = new Class1(0, "class1"); //I've created 1 main class (Class5) that 
Class2 b = new Class2(1, "class2"); //these 4 classes extend.
Class3 c = new Class3(2, "class3");
Class4 d = new Class4(3, "class4");

int randomNum = (int) (Math.random() *3);

Class5[] arrayForClasses = new Class5[]{a, b, c, d}; //since they're extending this
                                                    //class I want to make them into
                                                   //a single Array?


    String numberQuestion = JOptionPane.showInputDialog(null, 
"What question do you want to ask? \n 
Enter a number: \n 
1. First Question? \n 
2. Second Question? \n 
3. Third Question?");

int question = Integer.parseInt(numberQuestion); //not sure if this part is 
                                                //actually relevant at all??
                                               //Think it might be since I want to
                                              //use integers in my if statement below


if(question == 1){
    JOptionPane.showMessageDialog(null, "Blah blah"+arrayForClasses.getReturnValue()+" blah");
}

The .getReturnValue() method is within all of the classes (1-5). I'm not sure if this is actually what I have to do. But the problem I'm having is that when I compile it (even though it's not finished) it throws up a "CANNOT FIND SYMBOL" error with the message "symbol: method .getReturnValue() location: variable arrayForClasses type Class5[]". I'm just wondering where am I going wrong with this?

Any help is much appreciated.

Thanks!

Upvotes: 0

Views: 54

Answers (2)

Attila
Attila

Reputation: 28762

arrayForClasses is an array; it is not one of the classes it contains objects of, so it does not have a getReturnValue() method

You need to access an element of the array (that is an object of Class5 or one of its subclasses), and call getReturnValue() on that:

arrayForClasses[0].getReturnValue()

The index can go from 0 to 3 (total 4 elements) an you can use either one. You can even access all of them in a loop:

for (Class5 elem : arrayForClasses) { // cycles through each element in order
  elem.getReturnValue();
}

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

arrayForClasses is an array, and you can't ever add methods to arrays, only to the objects inside the arrays. You need to call the method on the objects in the array, not the array itself. So something like

arrayForClasses[0].getReturnValue()

Now, I say "something like" because I'm having a hard time following what you're trying to do, and I'm a little concerned about this idea of putting the "getReturnValue()" method into many different classes without a specific reason to do so.

Upvotes: 2

Related Questions