Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Gathering Data from a super class from a sub class

  1. #1
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Question Gathering Data from a super class from a sub class

    Maybe Super/Sub class might not be the best words for the title but that is the best I can come up with, with my knowlegde.

    So, my problem is that I was programming a game and I decided that I wanted to make the Player it's own object, which I was planning on doing after I got the mechanics down. My problem is that there are some variables that I want to keep in the main game and not in the player's class/object.

    At the moment I have a Game class and a player class. Game class has a variable that controls the location of the player. The player's class should be able to control that variable in the Game class. I would use my normal return statement in this case, but I think there is a simplier method to this.

    Here is an example.

    Java Code:
    public class Game() {
    	int playerX = 40;
            public static void main() {
    		Player player = new Player();
    	}
    }
    public class Player {
    	while(true) {
    	sleep(1000);
    	*insert something here*.playerX++;
    }
    What do I insert at the *insert something here* in order to allow my sub class to add 1 to int playerX?

    Thank you for your time.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    First, from your description it does appear that a Player should be responsible for computing and knowing its own position. Game could then quesry that position from the Player object.

    If however the computation does need to be in the Game class, you can pass a reference to the Game to the constructor of the Player.
    Java Code:
    // in Game
    Player player = new Player(this);
    
    // in Player
    
    final Game game;
    public Player(Game game) {
        this.game = game);
    }
    
    ...
    game.playerX++;
    db
    kammce likes this.

  3. #3
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    Thank you very much for taking your time to help me. This worked splendidly! Now I have three methods of doing this.
    Now, off to more programming!
    My API:
    Java Code:
    cat > a.out || cat > main.class

Similar Threads

  1. problem in set & get method of a super class
    By elenora in forum Advanced Java
    Replies: 5
    Last Post: 04-22-2011, 01:42 PM
  2. Referring to a super class
    By blug in forum New To Java
    Replies: 7
    Last Post: 03-20-2011, 05:06 AM
  3. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  4. super instanceof Class?
    By mikeiz404 in forum New To Java
    Replies: 11
    Last Post: 01-23-2009, 07:23 PM
  5. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •