Results 1 to 11 of 11
  1. #1
    Zorrent12 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Error with inheritance when adding packages

    I'm learning java from the tutorial on oracle. As I'm going along, I'm adding what I learn to a game prototype I'm working on. I recently reached the packages section and added the following to each of my files:

    Java Code:
    package com.haliglond.demo;

    After trying to recompile the files, I got several errors suggesting the class I tried to compile doesn't inherit the fields from its series of super classes. I'm sure the name in the package statements is consistent, as I copied and pasted it. Can someone explain what I did wrong?

    Here's what the file I tried to compile looks like:

    Java Code:
    package com.haliglond.demo;
    import java.util.Random;
    import java.awt.Rectangle;
    public class Orc extends NonPlayerCharacter {
    
         Random hitGenerator = new Random();
    
         public Orc(String selectedName) {
              name = selectedName;
              id = ++numberofCharacters;
              target = null;
              hitBox = new Rectangle(3, 3);
              attackable = true;
              dead = false;
    ...

    Here's what the NonPlayerCharacter class looks like:

    Java Code:
    package com.haliglond.demo;
    public class NonPlayerCharacter extends Character {
         
    }
    And here's what the Character class looks like:

    Java Code:
    package com.haliglond.demo;
    import java.awt.Rectangle;
    public abstract class Character {
    
         protected static long numberofCharacters = 0;
    
    //Basic fields
         protected String name;
         protected long id;
         protected Character target;
         protected Rectangle hitBox;
         protected boolean attackable;
         protected boolean dead;
    ...

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

  3. #3
    Zorrent12 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Actually, I believe I have. My files are in a series of folders that match the package name (com/haliglond/demo) and my classpath is set to that folder, but I'm still getting errors.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Would you mind posting the rest of your code, or an SSCCE, so I can try to reproduce your issue?

    -Gary-

  5. #5
    Zorrent12 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Alright, here's the code. I've cut out some of the methods and a couple fields that I'd like to keep private. However, I've tried compiling files that contain just this code and got the same error.

    .../com/haliglond/demo/Orc.java:

    Java Code:
    package demo;
    import java.awt.Rectangle;
    public class Orc extends NonPlayerCharacter {
    
         public Orc(String selectedName) {
              name = selectedName;
              id = ++numberofCharacters;
              target = null;
              hitBox = new Rectangle(3, 3);
              attackable = true;
              dead = false;
              faithLevel = 0;
              magicLevel = 0;
              resistanceLevel = 3;
              staminaLevel = 3;
              strengthLevel = 5;
              cutBonus = 5;
              slashBonus = 5;
              smashBonus = 3;
              stabBonus = 0;
              fireBonus = 0;
              iceBonus = 0;
              thunderBonus = 0;
              cutResistanceBonus = 0;
              slashResistanceBonus = 0;
              smashResistanceBonus = 0;
              stabResistanceBonus = 0;
              fireResistanceBonus = 0;
              iceResistanceBonus = 0;
              magicResistanceBonus = 0;
              thunderResistanceBonus = 0;
              lightResistanceBonus = 0;
              shadowResistanceBonus = 0;
              bleedResistanceBonus = 0;
              decayResistanceBonus = 0;
              diseasePoisonResistanceBonus = 0;
              maxHealth = staminaLevel * 25;
              currentHealth = maxHealth;
         }
    }

    .../com/haliglond/demo/NonPlayerCharacter.java:

    Java Code:
    package demo;
    public class NonPlayerCharacter extends GameCharacter {
         
    }

    .../com/haliglond/demo/Character.java:

    Java Code:
    package demo;
    import java.awt.Rectangle;
    public abstract class GameCharacter {
    
         protected static long numberofCharacters = 0;
    
    //Basic fields
         protected String name;
         protected long id;
         protected Character target;
         protected Rectangle hitBox;
         protected boolean attackable;
         protected boolean dead;
    //Stats
    //Experience fields are implemented for player characters
         protected int faithLevel;
         protected int magicLevel;
         protected int resistanceLevel;
         protected int staminaLevel;
         protected int strengthLevel;
    //Stat bonuses
         protected int faithBonus;
         protected int resistanceBonus;
         protected int magicBonus;
         protected int staminaBonus;
         protected int strengthBonus;
    //Offensive bonuses
    //NOTE: Do not include a magic bonus
         protected int cutBonus;
         protected int slashBonus;
         protected int smashBonus;
         protected int stabBonus;
         protected int fireBonus;
         protected int iceBonus;
         protected int thunderBonus;
    //Resistance bonuses
         protected int cutResistanceBonus;
         protected int slashResistanceBonus;
         protected int smashResistanceBonus;
         protected int stabResistanceBonus;
         protected int fireResistanceBonus;
         protected int iceResistanceBonus;
         protected int magicResistanceBonus;
         protected int thunderResistanceBonus;
         protected int lightResistanceBonus;
         protected int shadowResistanceBonus;
         protected int bleedResistanceBonus;
         protected int decayResistanceBonus;
         protected int diseasePoisonResistanceBonus;
    //Total stats
         protected int totalFaith;
         protected int totalResistance;
         protected int totalMagic;
         protected int totalStamina;
         protected int totalStrength;
     //Health
         protected int maxHealth;
         protected int currentHealth;
         
         public String getName() {
              return name;
         }
         
         public long getID() {
              return id;
         }
         
         public Character getTarget() {
              return target;
         }
         
         public boolean getAttackable() {
              return attackable;
         }
         
         public boolean getDead() {
              return dead;
         }
         
         public int getFaithLevel() {
              return faithLevel;
         }
         
         public int getMagicLevel() {
              return magicLevel;
         }
         
         public int getResistanceLevel() {
              return resistanceLevel;
         }
         
         public int getStaminaLevel() {
              return staminaLevel;
         }
         
         public int getStrengthLevel() {
              return strengthLevel;
         }
         
         public int getFaithBonus() {
              return faithBonus;
         }
    
         public int getResistanceBonus() {
              return resistanceBonus;
         }
    
         public int getMagicBonus() {
              return magicBonus;
         }
    
         public int getStaminaBonus() {
              return staminaBonus;
         }
    
         public int getStrengthBonus() {
              return strengthBonus;
         }
         
         public int getCutBonus() {
              return cutBonus;
         }
         
         public int getSlashBonus() {
              return slashBonus;
         }
         
         public int getSmashBonus() {
              return smashBonus;
         }
         
         public int getStabBonus() {
              return stabBonus;
         }
         
         public int getFireBonus() {
              return fireBonus;
         }
         
         public int getIceBonus() {
              return iceBonus;   
         }
    
         public int getThunderBonus() {
              return thunderBonus;
         }
         
         public int getCutResistanceBonus() {
              return cutResistanceBonus;
         }
         
         public int getSlashResistanceBonus() {
              return slashResistanceBonus;
         }
         
         public int getSmashResistanceBonus() {
              return smashResistanceBonus;
         }
         
         public int getStabResistanceBonus() {
              return stabResistanceBonus;
         }
         
         public int getFireResistanceBonus() {
              return fireResistanceBonus;
         }
         
         public int getIceResistanceBonus() {
              return iceResistanceBonus;
         }
         
         public int getThunderResistanceBonus() {
              return thunderResistanceBonus;
         }
         
         public int getLightResistanceBonus() {
              return lightResistanceBonus;
         }
         
         public int getShadowResistanceBonus() {
              return shadowResistanceBonus;
         }
         
         public int getBleedResistanceBonus() {
              return bleedResistanceBonus;
         }
         
         public int getDecayResistanceBonus() {
              return decayResistanceBonus;
         }
         
         public int getDiseasePoisonResistanceBonus() {
              return diseasePoisonResistanceBonus;
         }
         
         public int getTotalFaith() {
              return totalFaith;
         }
    
         public int getTotalResistance() {
              return totalResistance;
         }
    
         public int getTotalMagic() {
              return totalMagic;
         }
    
         public int getTotalStamina() {
              return totalStamina;
         }
    
         public int getTotalStrength() {
              return totalStrength;
         }
         
         public void setTarget(Character selectedTarget) {
             target = selectedTarget;
         }
    }
    EDIT: I just did some testing and found that it's confusing my Character class with a java.lang class. I changed it to GameCharacter, and got the following error when compiling NonPlayerCharacter.java:

    NonPlayerCharacter.java:2: cannot find symbol
    symbol: class GameCharacter
    public class NonPlayerCharacter extends GameCharacter {


    1 error
    Last edited by Zorrent12; 12-14-2010 at 12:11 AM.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You gave me your Character class twice, and I don't see your Orc class. I also don't see GameCharacter anywhere, which is what your compile error talks about. And it says that's in NonPlayerCharacter.java -- do you have another copy of NonPlayerCharacter.java lying around somewhere? Or maybe there's a NonPlayerCharacter.class file somewhere that needs to be deleted.

    -Gary-

  7. #7
    Zorrent12 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Actually, that was just a mistake while posting. I've updated the post. Sorry. Also, I don't have another NonPlayerCharacter class around.

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Well, now your Orc class just says package demo; rather than package com.haliglond.demo; -- is that how it is on your real code?

    -Gary-

  9. #9
    Zorrent12 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    At this point it is. I tried removing some of the earlier parts of the package name in my files to see if that would do anything. However, that didn't do much.
    If you want, I can upload the files themselves onto a site for you to download.
    Last edited by Zorrent12; 12-14-2010 at 12:15 AM.

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Well, GameCharacter needs to be in a file called GameCharacter.java. With that fix, and with the fix of the package name in Orc.java, it compiles for me without complaint.

    -Gary-

  11. #11
    Zorrent12 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Getting error with table packages..
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 10:20 AM
  2. packages
    By fogus in forum New To Java
    Replies: 1
    Last Post: 03-24-2009, 06:14 AM
  3. Replies: 3
    Last Post: 02-12-2008, 11:03 AM
  4. Using packages
    By prfalco in forum New To Java
    Replies: 5
    Last Post: 01-31-2008, 10:38 PM
  5. packages
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 12:10 PM

Tags for this Thread

Posting Permissions

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