I have finished studying the basics of object-oriented principles(e.g. composition, inheritance, polymorphism, abstract classes, and interfaces) in my book so I have decided to try and put everything together by making a template for a role playing game. At the current moment, I am not sure if my design is going in the right direction so I was hoping that you all could give me any feedback and tips.
Here are some of the abstract classes and an interface that I'm working on at the moment:
MeleeFighter.java
Knight.java
Attack.java
Weapon.java
Sword.java
Here are a couple of derived classes that I'm working on:
WhiteKnight.java
SmallSword.java
For the abstract classes, my questions at the moment are:
1)Knight.java - Should this class implement the Attack interface, or should I allow each of my derived classes to implement it? At this point, I'm pretty sure that any derived knight classes that are created will be able to attack with some type of weapon so it makes sense right now to keep the current implementation.
2)Weapon.java - As you can see above, this class has absolutely no functionality so is it redundant at this point? The reason I figured I create it now is because the book tells me that I may want to add functionality to an abstract class later on.
3)Sword.java - Likewise, you can see that this class also has no functionality and it is a derived class of Weapon. So, is this class also redundant?
For the derived classes, my questions at the moment are:
1)WhiteKnight.java - For the method public void attack(Weapon w), WhiteKnight can currently attack with any Weapon, but I would like to restrict this to only one-handed swords. Is there a way I can do some sort of downcast in the parameter to allow this because I will receive a compile time error if I do not use the exact parameter Weapon w that WhiteKnight implemented from the Attack interface?
Also, I'm aware that if I called WhiteKnight's attack() method in main, I could just use polymorphism to attack with a SmallSword.
2)SmallSword.java - At the moment, I have SmallSword as a derived class of Sword but I was thinking of creating another abstract class called OneHandedSword and have SmallSword extend from that.
Should I do this or continue to let it extend from Sword?
If you have read through all of this, I appreciate any feedback you may have. Thanks in advance
Here are some of the abstract classes and an interface that I'm working on at the moment:
MeleeFighter.java
/** * This abstract class is the base class for all MeleeFighters. * */ public abstract class MeleeFighter { /** * Job classes can equip specific types of weapons and armors. */ public abstract void equip(); /** * Job classes can remove specific types of weapons and armors. */ public abstract void remove(); }
Knight.java
package rpg.jobclasses.meleefighters.knights; import rpg.jobclasses.meleefighters.MeleeFighter; import rpg.commands.attack.*; import rpg.weapon.*; /** * This abstract class is the base class for all knights. * */ abstract class Knight extends MeleeFighter implements Attack { public abstract void attack(Weapon w); }
Attack.java
package rpg.commands.attack; import rpg.weapon.*; /** * * Job classes that implement this interface will be able to use the attack command and attack with a specific weapon. * */ public interface Attack { /** * Allows a character to attack with a weapon. * @param w */ void attack(Weapon w); }
Weapon.java
package rpg.weapon; /** * This class is an abstract class for the various weapons that job classes can use. * There is no functionality associated with this class. * */ public abstract class Weapon { }
Sword.java
package rpg.weapon.swords; import rpg.weapon.*; /** * * This abstract class is the base class for all swords in the game. * There is no functionality associated with this class. * */ public abstract class Sword extends Weapon { }
Here are a couple of derived classes that I'm working on:
WhiteKnight.java
package rpg.jobclasses.meleefighters.knights; import rpg.weapon.Weapon; /** * This job class can use level 1 white magic, but it has lower strength than a PureKnight. * Can equip : OneHandedSword * */ public class WhiteKnight extends Knight { /** * Attack with a one handed sword. */ public void attack(Weapon w) { System.out.println("WhiteKnight attacks with " + w); } public void equip() { } public void remove() { } }
SmallSword.java
package rpg.weapon.swords.one_handed; import rpg.weapon.swords.Sword; public class SmallSword extends Sword { public String toString() { return "SmallSword"; } }
For the abstract classes, my questions at the moment are:
1)Knight.java - Should this class implement the Attack interface, or should I allow each of my derived classes to implement it? At this point, I'm pretty sure that any derived knight classes that are created will be able to attack with some type of weapon so it makes sense right now to keep the current implementation.
2)Weapon.java - As you can see above, this class has absolutely no functionality so is it redundant at this point? The reason I figured I create it now is because the book tells me that I may want to add functionality to an abstract class later on.
3)Sword.java - Likewise, you can see that this class also has no functionality and it is a derived class of Weapon. So, is this class also redundant?
For the derived classes, my questions at the moment are:
1)WhiteKnight.java - For the method public void attack(Weapon w), WhiteKnight can currently attack with any Weapon, but I would like to restrict this to only one-handed swords. Is there a way I can do some sort of downcast in the parameter to allow this because I will receive a compile time error if I do not use the exact parameter Weapon w that WhiteKnight implemented from the Attack interface?
Also, I'm aware that if I called WhiteKnight's attack() method in main, I could just use polymorphism to attack with a SmallSword.
public static void main(String[] args) { SmallSword ss = new SmallSword(); WhiteKnight wk = new WhiteKnight(); wk.attack(ss); }
2)SmallSword.java - At the moment, I have SmallSword as a derived class of Sword but I was thinking of creating another abstract class called OneHandedSword and have SmallSword extend from that.
public abstract class OneHandedSword{}
public class SmallSword extends OneHandedSword { //...methods go here }
Should I do this or continue to let it extend from Sword?
If you have read through all of this, I appreciate any feedback you may have. Thanks in advance
