views:

238

answers:

3

how can i access state varibale of class keyboard with object of class kalaplayer

   /**
     * An abstract class representing a player in Kala.  Extend this class
         * to make your own players (e.g. human players entering moves at the keyboard
         * or computer players with programmed strategies for making moves).
         */
        public abstract class KalaPlayer
        {
            /**


          * Method by which a player selects a move.
                 * @param gs The current game state
                 * @return A side pit

 number in the range 1-6
             * @throws NoMoveAvailableException if all side pits for the player are empty 
             * (i.e. the game is over)
             */
            public abstract int chooseMove(KalaGameState gs) throws NoMoveAvailableException;
        }



    public class KeyBoardPlayer extends KalaPlayer {
        /**
         * Method by which a player selects a move.
         * @param gs The current game state
         * @return A side pit number in the range 1-6
         * @throws NoMoveAvailableException if all side pits for the player are empty 
         * (i.e. the game is over)
         */
        public KalaGameState state;

        public KeyBoardPlayer()
        {
            System.out.println("Enter the number of stones to play with: ");
        try
        {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
         int key = Integer.parseInt(br.readLine());  
         state=new KalaGameState(key);




    //key=player1.state.turn;
        }
    catch(IOException  e)
    {
            System.out.println(e);
        }

        }

    public int chooseMove(KalaGameState gs) throws NoMoveAvailableException{
     return 0;
     }
     }

import java.io.IOException; import java.io.BufferedReader;

import java.io.InputStreamReader;

public class KalaGame {
KalaPlayer player1,player2;

public KalaGame(KeyBoardPlayer Player1,KeyBoardPlayer Player2)
    {   //super(0);

player1=new KeyBoardPlayer();
player2 = new KeyBoardPlayer(); 
//player1=Player1;
//player2=Player2;
//player1.state  ****how can i access the stae variable from Keyboard CLass using object from KalaPlayer 
key=player1.state.turn;




    }
    public void play()
    {
        System.out.println("Enter the number of stones to play with: ");
    try
        {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
         int key = Integer.parseInt(br.readLine());  
System.out.println(key);
        KalaGameState state=new KalaGameState(key);
        printGame();

        }
    catch(IOException  e)
{
            System.out.println(e);
        }

        }
+2  A: 

You can't.

There is no ability access members of a subclass from its parent. If there were, the following would cause mass chaos:

public class Vehicle
{

}

public class Car extends Vehicle 
{
    SteeringWheel _wheel = new SteeringWheel();

    public SteeringWheel getSteeringWheel { return _wheel; }

    public SteeringWheel setSteeringWheel(SteeringWheel value)
    {
        _wheel = value;
    }
}

public class Bicycle extends Vehicle
{

}

Then:

Vehicle myVehicle = new Bicycle();

// This call couldn't possibly work since Bicylce has no steering wheel
// and Vehicle isn't aware of what the type of the is/isn't.
SteeringWheel myWheel = myVehicle.getSteeringWheel();
Justin Niessner
I am a bit confused how polymorphism works. I have super class KAlaPLayer and subClass Keyboard and each instance of keyboardplayer needs to have an instance of KalaGAmeState.if i do polymorphim thenKalaPlayer player1=new KeyboardPLayer();kalaPLayer.state*****whwre state is teh instance of kalagamestae which is called in teh constructor of keyboardplayer.pls can anyone help.
fari
A: 

Justin is correct, but instead of accessing the variable directly you could create a virtual property or get method and call that in the base class and then just make sure that it's overridden to return the variable in the subclass.

ho1
Thank you I really appritiate you taking the time to help me..Problem solved.:)
fari
A: 

It is impossible to do.

But you can take the line:

public KalaGameState state;

and put it in the definition of the class KalaPlayer. This assumes that each subclass of KalaPlayer should have a state of itself.

You can then still set the state in your constructor for each instance you are creating.

So this is more a design problem than a problem with the language because your superclass should contain all of the information which is common to all subclasses.

MKroehnert
Thnak you Thank so much. I am sorry but I am new to this and it tends to get confusing.
fari
Inheritance is a hierarchical thing where each subclass adds more information to the one stored in the superclass.Therefore it is impossible to access data which is not specified in the class itself.And by declaring the variables as KalaPlayer you tell the compiler that only the information from KalaPlayer is available.
MKroehnert