Results 1 to 7 of 7
  1. #1
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Help with inheritance

    i'm posting this in here because it's related to a game i'm working on. i Hope that's okay.

    So, i want to know if it's possible to create a class that can refer to multiple classes. Ill give an example. Say i create a class called Enemy or whatever and it controls the basic behaviors of all the enemies in a game. I then create a class called Tank and it extends Enemy. Is there a way to create an Enemy object in the main class and refer to the Tank class from the Enemy class? I hope this makes sense. Thanks in advance.

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

    Default Re: Help with inheritance

    Quote Originally Posted by waker3210 View Post
    i'm posting this in here because it's related to a game i'm working on. i Hope that's okay.
    This is fine.


    So, i want to know if it's possible to create a class that can refer to multiple classes. Ill give an example. Say i create a class called Enemy or whatever and it controls the basic behaviors of all the enemies in a game. I then create a class called Tank and it extends Enemy.
    So far, so good.


    Is there a way to create an Enemy object in the main class and refer to the Tank class from the Enemy class? I hope this makes sense.
    Hm, doesn't make sense to me. Can you elaborate? Are you saying that you want a parent class to refer to one of it's children? If so, no, you should never do this even if you could. Why don't you tell us what behavior you're trying to get rather than what code you're trying to use to get it?

  3. #3
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Re: Help with inheritance

    Okay, well i want to only have to create one ArrayList for all the different enemies, but still somehow be able to keep track of which enemy is which. I'm not sure if this is possible, and if not then oh well. This may not have anything to do with inheritance, but regardless, is there a way to do this. I hope this helps a little more.

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

    Default Re: Help with inheritance

    Quote Originally Posted by waker3210 View Post
    Okay, well i want to only have to create one ArrayList for all the different enemies,
    Sure you can have an ArrayList<Enemy> and fill it with children of the Enemy parent class or interface.

    but still somehow be able to keep track of which enemy is which.
    Just what do you mean by "keep track of which enemy is which"? The Enemy object itself will know what type it itself is.

  5. #5
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Re: Help with inheritance

    Ah, so i can make an ArrayList<Enemy> but add a Tank class to it instead of an Enemy class?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Help with inheritance

    You don't add classes; you add instances of classes. If Tank extends Enemy, every Tank is-a Enemy.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Re: Help with inheritance

    I know you don't really add classes, but i didnt realize that Tank is a Enemy. Thanks a bunch, lol, i feel dumb though.

Similar Threads

  1. Inheritance
    By new_2_j in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 05:56 PM
  2. JPA Inheritance
    By videanuadrian in forum Advanced Java
    Replies: 0
    Last Post: 01-11-2011, 06:30 AM
  3. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 10:41 AM
  4. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 02:53 PM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 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
  •