Java Super and Sub Classes

Sharky Forums


Results 1 to 6 of 6

Thread: Java Super and Sub Classes

  1. #1
    Goldfish
    Join Date
    Jun 2001
    Location
    Savannah GA, USA
    Posts
    86

    Java Super and Sub Classes

    Ok so i've got a game, with class Player, and a few extensions of Player

    i've greatly over simplified my code for ease of my typing.

    Code:
    class Player
        public PlayerStuff;
    class SmartPlayer extends Player
        public SmartStuff;
    class HumanPlayer extends Player;
        Public HumanStuff;
    then in my game class i'm trying to define an array of players that is a mix of players, smartplayers and humanplayers.
    Code:
    Player[NUM_OF_PLAYERS] PlayersInGame;
    
    PlayersInGame[1] =  new Player;
    PlayersInGame[2] = new SmartPlayer;
    and the problem arises that i cant access the methods in SmartPlayer even if instances of the array is pointed at a SmartPlayer... any ideas?
    ACK I think i blew it up...

  2. #2
    Hammerhead Shark
    Join Date
    Aug 2007
    Location
    King George, Virginia
    Posts
    1,429
    The reason for that is that the compiler has no way of guaranteeing that a reference in an array of Players has the methods within the SmartPlayer class. Since you could just as easily store a HumanPlayer there that does not have the method, the compiler forces you to make such a guarantee.

    There are two ways around this.
    1) I call this the wrong way, but sometimes you can't avoid it. You could force the instance to be a smart player with a cast, so something like:
    Code:
    ((SmartPlayer)PlayersInGame).methodInSmartPlayer();
    To be even safer, you could do an instanceof check, so that you never get a ClassCastException if you accidentally do that with the wrong class.

    2) Only call methods that are guaranteed to be common among Player - if the method you are trying to call can be made abstract in Player and then implemented in both, then by all means take this route.
    Current Rig: Q6600 @2.8GHz | Zalman CNPS 9700 | 6 GB Corsair XM2 DDR2 RAM @ 736 MHz | 2 x 150 GB Raptors Raid 0 | Asus Dark Knight 4870 512 MB | Gigabyte EP45-U3DL Motherboard | ASUS VW266H Panel | Lian Li PC-V1200Bplus

    XBL Gamertag: Recursivity
    http://xclite.net

    Now Playing: Left4Dead, Team Fortress 2, Killing Floor, Dawn of War 2

  3. #3
    Goldfish
    Join Date
    Jun 2001
    Location
    Savannah GA, USA
    Posts
    86
    can you typecast as things other then default types?
    ACK I think i blew it up...

  4. #4
    Hammerhead Shark
    Join Date
    Aug 2007
    Location
    King George, Virginia
    Posts
    1,429
    Yes indeed - and that TypeCast will actually work, provided what you are casting actually was instantiated as a SmartPlayer. If you tried the cast on a HumanPlayer, it would throw an exception during execution.
    Current Rig: Q6600 @2.8GHz | Zalman CNPS 9700 | 6 GB Corsair XM2 DDR2 RAM @ 736 MHz | 2 x 150 GB Raptors Raid 0 | Asus Dark Knight 4870 512 MB | Gigabyte EP45-U3DL Motherboard | ASUS VW266H Panel | Lian Li PC-V1200Bplus

    XBL Gamertag: Recursivity
    http://xclite.net

    Now Playing: Left4Dead, Team Fortress 2, Killing Floor, Dawn of War 2

  5. #5
    Goldfish
    Join Date
    Jun 2001
    Location
    Savannah GA, USA
    Posts
    86
    awesome, thanks a bunch.
    ACK I think i blew it up...

  6. #6
    Hammerhead Shark
    Join Date
    Aug 2007
    Location
    King George, Virginia
    Posts
    1,429
    No problem.
    Current Rig: Q6600 @2.8GHz | Zalman CNPS 9700 | 6 GB Corsair XM2 DDR2 RAM @ 736 MHz | 2 x 150 GB Raptors Raid 0 | Asus Dark Knight 4870 512 MB | Gigabyte EP45-U3DL Motherboard | ASUS VW266H Panel | Lian Li PC-V1200Bplus

    XBL Gamertag: Recursivity
    http://xclite.net

    Now Playing: Left4Dead, Team Fortress 2, Killing Floor, Dawn of War 2

Posting Permissions

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