Results 1 to 11 of 11
- 12-13-2010, 09:43 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
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:
And here's what the Character class looks like:Java Code:package com.haliglond.demo; public class NonPlayerCharacter extends Character { }
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; ...
- 12-13-2010, 10:10 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Maybe you haven't gotten to this page yet:
Managing Source and Class Files (The Java™ Tutorials > Learning the Java Language > Packages)
-Gary-
- 12-13-2010, 10:32 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
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.
- 12-13-2010, 10:34 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Would you mind posting the rest of your code, or an SSCCE, so I can try to reproduce your issue?
-Gary-
- 12-13-2010, 10:50 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
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:
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: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; } }
NonPlayerCharacter.java:2: cannot find symbol
symbol: class GameCharacter
public class NonPlayerCharacter extends GameCharacter {
1 errorLast edited by Zorrent12; 12-14-2010 at 12:11 AM.
- 12-13-2010, 11:45 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 12-13-2010, 11:51 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
Actually, that was just a mistake while posting. I've updated the post. Sorry. Also, I don't have another NonPlayerCharacter class around.
- 12-14-2010, 12:09 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 12-14-2010, 12:11 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
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.
- 12-14-2010, 12:13 AM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 12-14-2010, 12:35 AM #11
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Getting error with table packages..
By doha786 in forum New To JavaReplies: 2Last Post: 03-31-2010, 10:20 AM -
packages
By fogus in forum New To JavaReplies: 1Last Post: 03-24-2009, 06:14 AM -
Importing packages from the packages within same application.
By sta2003 in forum New To JavaReplies: 3Last Post: 02-12-2008, 11:03 AM -
Using packages
By prfalco in forum New To JavaReplies: 5Last Post: 01-31-2008, 10:38 PM -
packages
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 07-31-2007, 12:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks