Java Programming Question

Post » Fri Jan 31, 2014 3:36 am

I'm trying to learn Java. Now I have a little question.

I have set up a very primitive Inventory-system tied to a "Character"-object that I made myself and there are a couple of methods to check the various attributes of the items. All this works.

However, I encounter problems when I wanted to have an additional Item-type, called Weapon.

My initial idea was to create a new Weapon-class which inherited everything from the Item-class. I added some extra methods and attributes for damage and weapon usage here.

Now the problem appears after I have added my Weapon-object into the inventory (an ArrayList of the "Item"-type).

I want to print some text which displays the various properties of the item. Doing this to normal items is no problem, but it doesn't work for Weapons because of the methods added to the Weapon class that are not found inside the Item class.

I don't need any code. I just want someone to explain to me roughly how to achieve something like this. Maybe I'm doing it completely wrong. If so, I want to be directed towards the proper way :wink:

User avatar
Carlos Vazquez
 
Posts: 3407
Joined: Sat Aug 25, 2007 10:19 am

Post » Thu Jan 30, 2014 10:22 pm

Weapon needs to have its own toString method since you want to print out the stuff from it. Otherwise, it's going to use the toString method from Item.
User avatar
Katie Louise Ingram
 
Posts: 3437
Joined: Sat Nov 18, 2006 2:10 am


Return to Othor Games