Re: Help with inheritance
Quote:
Originally Posted by
waker3210
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.
Quote:
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.
Quote:
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?
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.
Re: Help with inheritance
Quote:
Originally Posted by
waker3210
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.
Quote:
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.
Re: Help with inheritance
Ah, so i can make an ArrayList<Enemy> but add a Tank class to it instead of an Enemy class?
Re: Help with inheritance
You don't add classes; you add instances of classes. If Tank extends Enemy, every Tank is-a Enemy.
db
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.