Page 3 of 3 FirstFirst 123
Results 41 to 48 of 48
  1. #41
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    What else do you need? I posted the code in question, and the results it gave me. I read about casting, got examples, tried various ways of doing what was suggested. None of them corrected the error I was getting, which is what I described above.

  2. #42
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Array of Objects (Superclass containing Subclass)

    Java Code:
    public static void main(String[] args){
        Hero hero = new Hero();
        Weapon sword = new Weapon("Sword", 4.0, 5, "Sword", "Iron", 1, "Main");
        hero.inventory.addItem(sword);
        System.out.println(hero.inventory.inventory[0].weapondamage);
     }
    Where in there are you casting the Item (which is what the inventory holds) into a Weapon when you try and access the weapondamage?

  3. #43
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    I took it out because it didn't change the errors that I was getting. Everything I posted was what I had coded before any suggestions. I tried casting when I created the weapon, i tried to cast the inventory slot that the weapon was going to be added to, in the addItem method.

  4. #44
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Array of Objects (Superclass containing Subclass)

    Quote Originally Posted by stupidwaldo View Post
    I tried casting it how you said

    System.out.println(hero.inventory.(Weapon)inventor y[0].weapondamage);

    and it gives me all sorts of errors
    You need to cast what's returned by the inventory array before you call weopandamage.

  5. #45
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    That would mean in here then:
    Java Code:
    public void addItem(Item i){
    		Item[] temp = new Item[inventory.length];
    		for (int a = 0; a < inventory.length; a++)
    			temp[a] = inventory[a];
    		inventory = new Item[(inventory.length) + 1];
    		for (int a = 0; a < temp.length; a++)
    			inventory[a] = temp[a];
    		inventory[inventory.length - 1] = i;
    	}
    I tried to cast in various places here, but the error remained the same
    Last edited by pbrockway2; 12-09-2011 at 11:55 PM. Reason: code tags added

  6. #46
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Array of Objects (Superclass containing Subclass)

    No.
    You cast it when you use it.

    Java Code:
    (SubClass)superClassReference.someSubClassMethod();

  7. #47
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    Well I use it here:

    System.out.println(hero.(Weapon)inventory.inventor y[0].weapondamage);

    I get the following errors

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method println(boolean) in the type PrintStream is not applicable for the arguments (Hero, Weapon)
    Syntax error on token ".", , expected
    inventory cannot be resolved to a variable

  8. #48
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Array of Objects (Superclass containing Subclass)

    Because that is invalid syntax.
    (Weapon)(hero.inventory.inventory[0]).weapondamage
    The result of the bit in brackets is what you are casting to Weapon.
    Which we could have told you if you had shown us the code, rather than telling us that you had done as we had suggested.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Override a superclass's methods with a subclass
    By zach&kody in forum New To Java
    Replies: 7
    Last Post: 05-24-2011, 02:50 PM
  2. subclass calling superclass method
    By stackptr89 in forum New To Java
    Replies: 10
    Last Post: 11-22-2010, 07:40 PM
  3. Using superclass fields in subclass method
    By lonegreyride in forum New To Java
    Replies: 12
    Last Post: 11-17-2010, 01:21 PM
  4. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 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
  •