Results 1 to 3 of 3
  1. #1
    Nyxeka is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Question Have a question about Interfaces abstract classes and arraylists...

    Alright, here we go.

    I am learning java, and attempting to write a text-based RPG game.

    So, in this game, there will be many items. I have an abstract class for item, and each item in the game will have it's own subclass, that extends item (hopefully).

    I also have an inventory class, that various entities in the game will be able to use...

    For now, each item has an ID, a name, and an amount.

    In the inventory class, I want to be able to have an arraylist of item subclasses, so I have:

    Java Code:
    public abstract class Item {
    	public int ID;
    	// With every item, there is at least one of it.
    	public int amount = 1;
    	public String name = "null";
    }
    And in the inventory class:

    Java Code:
    List<Item> itemList = new ArrayList<Item>();
    
    public void addItem(Item itemName, int amount) {
    		
    		if ((itemList.size() < maxItems)) {
    			itemList.add(itemName);
    			// since the the list size in the same index as the last item in the list...
    			itemList.get(itemList.size()).amount = amount;
    		} else {
    			// Eventually put some kind of error message here.
    			//System.out.println("inventory full.");
    		}
    	}
    so if say, I have a sword subclass with it's own field and methods, would I be able to:

    Java Code:
    public class sword extends Item {
         // various field and methods in here...
    }
    
    public sword swordItem;
    swordItem.variables = such and such
    inv.addItem(swordItem,1);
    I am unsure, but I think I need to have Item be an interface, except for the fact that I need to be able to change the fields in the item class, and an interface makes them all final...

    -Thanks!

    -Nyxeka

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Have a question about Interfaces abstract classes and arraylists...

    An interface has no fields to speak of (the final ones are really constants, not fields). Whether you need an interface or an abstract class will depend on several things. If your concrete classes will need to implement multiple different behaviors (i.e., need to be Item-like and also Animal-like), you'll tend to bend towards interfaces. If an Item is only an Item, and if you want to give all Items some default behaviors, then an abstract class is the way to go. Regardless, you'll not want to have outside classes directly mucking around with an objects fields, so whether you use an abstract class or an interface, outside classes will only interact with the public methods of the concrete classes.

  3. #3
    Nyxeka is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Have a question about Interfaces abstract classes and arraylists...

    Alright, thanks for explaining that :)

Similar Threads

  1. Question about abstract classes and methods
    By mwr1976 in forum New To Java
    Replies: 8
    Last Post: 11-21-2011, 01:42 PM
  2. Alternative to extending classes from interfaces?
    By TacoManStan in forum New To Java
    Replies: 5
    Last Post: 09-10-2011, 03:31 AM
  3. Abstract Classes and ArrayLists.
    By Hooky75 in forum New To Java
    Replies: 9
    Last Post: 05-14-2011, 09:38 AM
  4. static import only from classes and interfaces
    By balajir in forum New To Java
    Replies: 3
    Last Post: 12-13-2010, 05:31 AM
  5. question about abstract classes
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 07-08-2009, 07:19 AM

Posting Permissions

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