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

    Default Array of Objects (Superclass containing Subclass)

    How can I make an array that can contain any subclasses of a certain superclass?
    Last edited by stupidwaldo; 12-08-2011 at 12:50 AM.

  2. #2
    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)

    Instances of the subclass ARE instances of the superclass. So you declare a variable to be Superclass[], and initialise it using new.

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

    Default Re: Array of Objects (Superclass containing Subclass)

    Nevermind. I thought that was the issue that I was having, but it turns out It was something else. Thank you anyway

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

    Default Re: Array of Objects (Superclass containing Subclass)

    However, a problem that I was having before has arisen again. I wish to call a type of data that is part of the subclass, but not part of the superclass. I get an error in my syntax when I attempt this. I would i be able to access this information??

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

    Posting your code would help us understand your problem. However, if your are attempting to do something like:
    Java Code:
    Foo f = new Bar();
    f.methodInBar();
    Then no you cannot do that as the f variable is of the Foo type and the Foo class does not have the methodInBar method.

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

    Default Re: Array of Objects (Superclass containing Subclass)

    I have

    Subclass sub = new Subclass(paramnotinsuperclass);

    Superclass array[] = new Superclass[num]

    array[].add(sub(stuff))

    var = array[num].paramnotinsuperclass

    and it gives me an error ^ directly above

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

    Yep, the same as my first reply except you are dealing with a variable and not a method. The Superclass class does not have a paramnotinsuperclass so therefore you cannot access it. What you need to do is cast your objects to the Subclass first.

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

    Default Re: Array of Objects (Superclass containing Subclass)

    How would i go about doing that?

  9. #9
    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 sorry I didn't realise my previous reply was invisible.

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

    Default Re: Array of Objects (Superclass containing Subclass)

    You still haven't answered my question, of how i would go about casting the objects

  11. #11
    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)

    Quote Originally Posted by stupidwaldo View Post
    How would i go about doing that?
    Do you mean how do you cast? Like this:

    Java Code:
    (Subclass)array[num].paramnotinsuperclass
    -----

    I have to say that variables in Java have a type. So if you say "Subclass sub" then sub is of type Subclass. And if you say "Superclass array[]" (better would be "Superclass[] array") then array is of type array of Superclass.

    There are other ways of doing things, but this is Java's way and it provides all sorts of compile time type checking: the compiler will ensure that if you have a variable of a specific type you will only do things with it - call its methods, pass it as an argument, access its elements - that are appropriate to that type. When you cast you defeat this process. The compiler will try to keep things as sane as it can (by ruling out any old random cast) but it will trust that if you say "(Subclass)array[num]" you know what you are doing. Things can and will go wrong at runtime if array[num] turns out not to be an instance of Subclass.

    So "(Subclass)array[num]" is the answer to "how do I cast?", but a better question might have been "how can I avoid using an array like this and having to cast?". The answer to that, of course, depends on what you are trying to do (rather than how you are trying to do it.)

  12. #12
    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
    You still haven't answered my question, of how i would go about casting the objects
    If you don't know then look it up. Sheeesh! Do we have to do everything.

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

    Default Re: Array of Objects (Superclass containing Subclass)

    Nevermind it just showed up now

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

    Default Re: Array of Objects (Superclass containing Subclass)

    The biggest problem that im going to have is that the array will have to be able to hold more than one different type of subclass, which works, and access the various bits of information, which doesn't work

  15. #15
    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 can use the instanceof operator.

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

    Default Re: Array of Objects (Superclass containing Subclass)

    Maybe i can clarify the issue a little. I am attempting to create a game (an rpg) and i am working on the inventory system. The line of code that has the issue is:

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

    inventory[] is an array of Item, and inventory[0] contains an Object of Weapon, which is a subclass of Item and weapondamage is a variable that is located only in Weapon. I need inventory to be able to handle other various subclasses of Item that will contain different variables

  17. #17
    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)

    None of that changes the advice you have been given.

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

    Default Re: Array of Objects (Superclass containing Subclass)

    Well i dont see how i can cast the inventory array to any one thing. How would i use the instanceof operator?

  19. #19
    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
    Well i dont see how i can cast the inventory array to any one thing.
    You don't cast the array, you cast the item in the array.
    How would i use the instanceof operator?
    Once again L O O K I T U P!

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

    Default Re: Array of Objects (Superclass containing Subclass)

    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

Page 1 of 3 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
  •