0

I am trying to call the methods from my subclass hangmanwords in hangman, my main class. This is the code:

public class hangManWords {
    //declares words
    String [] words = { "Highschool", "Government", "Continents", "Professors", "Programmer", "Dealership", "Controller", "Motorcycle", "Lightsaber"}; 

    public void randomizeWords(String [] words) {
        //randomize the words
        for (int i = words.length - 1; i > 0; i--) {
            //generate a random index
            int j = (int)(Math.random() * (i + 1));

            //swaps list i with list j
            String temp = words[i];
            words[i] = words[j];
            words[j] = temp;
        }
    }

    public  String getNextWord (String [] words) { //gets the next random word
        for (int i = 0; i < words.length; i++) {

            if (words[i] == null) {
                continue;
            }
            String temp = words[i];
            words[i] = null;
            return temp;
        }
        return null;

    }
}

Here is the part in my main, where I'm trying to use it:

randomizeWords(words); //randomly generates a word from the word list

//players first word
guessThisWord = getNextWord(words);
guessThisWord = hideWord(guessThisWord, originalWord);//hides the word with _
Zong
  • 6,160
  • 5
  • 32
  • 46
  • 3
    so what is the problem? – Raman Shrivastava Apr 19 '16 at 17:30
  • Instantiate your object and just call the methods on it, surely? `hangManWords hmw = new hangManWords(); hmw.randomizeWords(arrWords); String s = hmw.getNextWord(arrWords);` Not sure why you're passing in a String array to the methods. That doesn't make sense. You've already defined the words array inside the class itself. And capitalize your class names. Should be HangManWords. And do you really mean subclass here? Or do you mean child class? – ManoDestra Apr 19 '16 at 17:33
  • Are you getting an error on execution? Or are the results not what you expect? – Woodchuck Apr 19 '16 at 17:35

3 Answers3

0

Declare virtual methods in the base class, implement them in subclass. Now, if the object is of a subclass type, then all calls for those virtual methods in the methods declared in base class will invoke subclass methods.

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
0

The others have already mentioned how to cast objects to get an answer to your question, but asking that question in the first place points to a possible design problem. Some possible reasons:

  • The method is in the wrong place.
  • The code which calls the method is in the wrong place.
  • The subclass should not extend the other class. It's best to prefer composition over inheritance. And when inheriting, the code should follow the Liskov substitution principle.

  • The classes are non-cohesive, they have more than one responsibility, and they should be split into multiple classes.

Call a method of subclass in Java

Community
  • 1
  • 1
0

Do you mean like this?

public class Hangman {

    public static void main(String[] argv) {
        // Instantiate the class.
        hangManWords g = new hangManWords();

        // Call the methods...

        // g has access to `words` so no need for an argument.
        g.randomizeWords(); //randomly generates a word from the word list

        // g has access to `words` so no need for an argument.
        //players first word
        guessThisWord = g.getNextWord();

        // I don't see `hideWord` and `originalWord` defined, but assume they are somwehere.
        guessThisWord = g.hideWord(guessThisWord, originalWord); //hides the word with _
    }
}

Notes

You should consider starting class names with uppercase like HangManWords

totoro
  • 2,469
  • 2
  • 19
  • 23