Results 1 to 3 of 3
  1. #1
    senca is offline Member
    Join Date
    Feb 2010
    Rep Power

    Default Logical inheritance issue...


    I'm working on a small javagame but I ran into a wall. A small explanation: A character class, a number of subclasses starting with a warrior and a wizard. We also have an Item class with subclasses starting with ManaPotion and HealthPotion. All easy except for one thing: every character can carry items, no matter what kindoff item. But! only a wizard is allowed to use a mana potion. So I gave my superclass Character an abstract method useItem() but I don't have a single clue on how to implement this method in the warrior and wizard class so that only a wizard can use a ManaPotion. Also a HealthPotion logically restores health and not mana. (Mana is not a warrior variable but only used inside the wizard class) and so I'm also kinda stuck on how to make a difference between the 2 potions when using the useItem() method. I gave thus method a parameter 'Item item' so that an item can be assigned but this to gives that problem that when using this method, it also has to 'know' what kind off potion it is.
    If anyone can put me in the right direction, I would be thrilled. :)

    Last edited by senca; 06-13-2011 at 01:03 PM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Tucson, AZ
    Rep Power


    This is a really tough question to answer because the best solution depends on how much flexibility you want and how much time you're willing to spend writing it. At the simplest, you could have a big 2-dimensional array of booleans somewhere, indexed by character class and item number. For a bit more flexibility, you could give your character classes a canUse(Item item) method. Or you could give your item a isUsableBy(CharacterClass characterClass) method. Depending on your design, the second might make more sense.

    Java Code:
    boolean isUsableBy(CharacterClass characterClass) {
         return characterClass.usesMana(); // or whatever
    Get in the habit of using standard Java naming conventions!

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Rep Power


    If all items have its own class, you can also use instanceof. Doing a quick google I found instanceof : Java Glossary that seems to explain it rather well.

Similar Threads

  1. Inheritance issue
    By powerpravin in forum New To Java
    Replies: 3
    Last Post: 04-12-2011, 06:11 AM
  2. if block with logical or
    By Ranu in forum New To Java
    Replies: 6
    Last Post: 07-01-2010, 08:11 AM
  3. Logical Gates
    By lingz89 in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 01:11 AM
  4. Simple Inheritance issue...
    By AWE in forum New To Java
    Replies: 3
    Last Post: 07-27-2009, 09:56 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