0

I have a generic game class called RiverCrossingGame. The idea is that subclasses of this one will implement different river crossing puzzles.

In my superclass I have a Vector of states. Now these states are generic. The GenericState implements some behavior common in the concept of state for any river crossing game. The the subclass associated State will implement additional methods which are particular of the game.

So I have my RiverCrossingGame with the vector of states:

public abstract class RiverCrossingGame implements PuzzleGame {
    public GenericBank westBank;
    public GenericBank eastBank;
    public static Vector<GenericState> states;
    public List<String> steps;

    public abstract void play();
    public abstract void printSteps();
}

Then the WolfGoatCabbageGame I have defined a particular state for this game called State(it is in the Wolf, Goat and Cabbage game package) which is a subclass of the GenericState:

public class State extends GenericState {
    // State members...
}

Then in the class for the actual class I try to initialize the vector of states:

public class WolfGoatCabbageGame extends RiverCrossingGame {
    public enum Element { FARMER, WOLF, GOAT, CABBAGE };

    public WolfGoatCabbageGame() {
        westBank = new Bank("WEST", new Element[]{ Element.FARMER, Element.WOLF, Element.GOAT, Element.CABBAGE }, false);
        eastBank = new Bank("EAST", true);
        states = new Vector<State>();
        steps = new ArrayList<String>();
    }

    // more members...
}

And here's the problem. When initializing states I am getting this error:

Type mismatch: cannot convert from Vector to Vector

And here's where this got me. State is a subclass of GenericState. Why wouldn't I be able to initialize the Vector that way? Am I missing something obvious? What should I do?

dabadaba
  • 9,064
  • 21
  • 85
  • 155

1 Answers1

1

I think you need to initialize states as Vector<GenericState>. Once it's been initialized, you can populate it with instances of type State, as they are instances of the GenericState class.

Dan Forbes
  • 2,734
  • 3
  • 30
  • 60
  • Yeah that's what I ended up doing :) I was confused because for I moment I was afraid since they would be `GenericState` instances the `State` methods wouldn't be find. Then I realized I was wrong and they wouldn't be `GenericState` instances but `State` instances as I add them. – dabadaba Feb 01 '16 at 16:04
  • Glad that worked! If you feel like it makes sense, you should consider "Accepting" this answer. – Dan Forbes Feb 01 '16 at 16:06
  • I know, but there's a timer and I cannot upvote right now. As soon as I can I will. – dabadaba Feb 01 '16 at 16:09