Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48
  1. #21
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Array of Objects (Superclass containing Subclass)

    We don't read minds.

  2. #22
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    I looked up how I could use casting and the instanceof operator, and the method that I tried didn't work in the situation that I have. Any suggestions?

  3. #23
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Array of Objects (Superclass containing Subclass)

    Not knowing how your classes are structured it's hard to say where you're going wrong, but I suspect it's either not letting classes do the work they should be responsible for (ie Items should have a use(target) method maybe?), or you should have an Inventory class which simply holds lists of different sorts of things...ie stuff that can be used as a weapon, stuff that can be consumed, etc etc.

    It's quite possible you have simply over-objectified your code, creating an inheritance tree that doesn't work for the problem at hand.

  4. #24
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    I have classes structured like this:

    public Item(String Itemname, double Itemweight)

    public Weapon(String Itemname, double Itemweight, int damage, String material, String weapontype, int handnumber, String handuse)

    and there will be various others (like consumables) that i haven't created yet because of this snag. I need to create an array that can hold all of these different subclasses, and i need to be able to access the information within them. Right now, i have an array of Item and i add the Weapon to the arrray and I can only access the information that they have in common.

  5. #25
    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)

    When you used instanceof what did you code up?

  6. #26
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    I looked up how to use instanceof in here, to check what i was and then tried to cast i to be the subclass that it got

    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;
    }

    Unfortunately it didn't work. I'm a little stumped as to what i should be doing

  7. #27
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Array of Objects (Superclass containing Subclass)

    :headdesk:

    What does "didn't work" mean? There could an infinite number of reasons it didn't work and we do not have the time to guess which one applies in your case. How about providing more details such as error messages.

  8. #28
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    I'm having the same problem I've been having. When i try to get information from one of the subclasses stored in the array of the superclass, it doesn't register that the variables exist. I can only access the information that the superclass and the subclass have in common

  9. #29
    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)

    Look all Java Objects inherit from Object, now look at this

    Java Code:
    Object myButton = new JButton();
    System.out.println(myButton instanceof JButton);
    Now Imagine testing for your subclasses in your array using instanceof

  10. #30
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    Using this in my testing only confirms that it really is an instance of my subclass. However, the data contained within the subclass is still inaccessible when an instance of the subclass is stored in an array of the superclass.

    Weapon sword = new Weapon("Sword", 4.0, 5, "Sword", "Iron", 1, "Main");

    The problem is that Weapon has the following data while

    Item sword = new Item("Sword", 4.0);

    is all the information that the superclass holds. So all of the other pieces of data cannot be accessed

  11. #31
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Array of Objects (Superclass containing Subclass)

    OMFG!

    Are you paying attention?

  12. #32
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    Apparently not. I tried the solutions that you described to me, to the best of my abilities, and none of them worked. The only quick solution to this that I can see would be to use an array of pointers. However, every source i've looked at tells me that Java has no pointers, so I have no other ideas

  13. #33
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Array of Objects (Superclass containing Subclass)

    You check the object using instanceof then you cast it. How hard is that to understand?

  14. #34
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    I tried that. I checked the object, cast it to the subclass that I needed it to be. Nothing changed. I still can't get the information that i needed to because the array that the object is in is the superclass. Casting that individual item within the array affected nothing

  15. #35
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Array of Objects (Superclass containing Subclass)

    I'm outta here!

  16. #36
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array of Objects (Superclass containing Subclass)

    There's a certain level of frustration here... What follows is just my 2c. Rereading it before posting I am amazed at how much... bulk, anyway... you can still get for 2c.

    @OP: In the revised version of your question (#4) you asked how you might access member variables and methods of a subclass when you have a variable or expression whose type is that of the superclass. And you received the answer (#7): you cast the variable or expression to the subclass type. That's not an answer, it's the answer.

    After that it was all down hill, much to the detriment of Junky's desk. But that's OK! We wouldn't be here if we didn't have sturdy desks.

    Possibly there are a couple of things you might do to reduce the frustration level. First, rather than saying "I tried that", "I tried the solutions", "Using this" and variations, post the actual code. By far the best is code that compiles, or at least complete enough that compilation could be attempted, even if you have to go to the trouble of constructing a small example that is free of all the details of your program that are extraneous to your problem. (SSCCE)

    And not just any old code: post the actual code you used. In #29 al_Marshy_1981 asked "When you used instanceof what did you code up?". But read your reply (#30). It does not contain any use of instanceof nor any cast such as you said in that post you tried.

    Secondly, describe explicitly what the result of the code you used was. Not "nothing changed", "none of them worked", "it doesn't register" and all the rest, but complete compiler messages, and actual descriptions of program behaviour (both output and full stack traces in the case runtime errors).

    Again, read Junky's #27 "How about providing more details such as error messages." then your response (#28) which provides us with nothing from either the compiler or the runtime.

    Both these points stress precision and a strict adherence to "just the facts": things which should dominate in a technical discussion. To that end we need to clear away the madeup lexicon ("registering" that variables exist, classes having information "in common" etc) especially where the terms are used speculatively (array of "pointers" as a solution). And replace it with code, and a description of observed behaviour when that code is compiled and run.

  17. #37
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Array of Objects (Superclass containing Subclass)

    I smell troll. Either that or OP is living up to their username.

    P.S. I wanna headdesk smilie (or not so smilie).

  18. #38
    stupidwaldo is offline Member
    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Default Re: Array of Objects (Superclass containing Subclass)

    I apologize if I have been unclear. Ill post the code in question:

    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);
    }

    I highlighted the problem area. It says that weapondamage cannot be resolved or is not a field

    public Weapon(String in, double iw, int d, String m, String wt, int hn, String hu){
    super(in, iw);
    weapondamage = d;
    weaponmaterial = m;
    weapontype = wt;
    handnumber = hn;
    handuse = hu;
    }

    This is the code through which weapon gets its values

    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;
    }

    This is the code that creates the inventory and adds an item to it

  19. #39
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Array of Objects (Superclass containing Subclass)

    Quote Originally Posted by stupidwaldo View Post
    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;
    }
    Holy cr@p that's inefficient.

  20. #40
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Array of Objects (Superclass containing Subclass)

    There's no need to apologise.

    I don't think my post was unclear. Blunt perhaps (and I would apologise if that gave offence), but not unclear. Read it. It says what needs to be posted in order for the discussion to make progress: code (complete and free of extraneous detail), and a description of observed result (compiler messages, output and runtime error messages). That is not what you have posted.

    It also says what the answer is: casting the expression whose type is a superclass of what you expect the runtime type to be. Although the suggestion has been made before, read up about casting (including the example given in this thread, #11) and try that.

Page 2 of 3 FirstFirst 123 LastLast

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
  •