Results 1 to 7 of 7
Thread: Help with inheritance
- 05-19-2012, 04:20 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
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.
-
Re: Help with inheritance
This is fine.
So far, so good.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.
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?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.
- 05-19-2012, 07:01 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
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
Sure you can have an ArrayList<Enemy> and fill it with children of the Enemy parent class or interface.
Just what do you mean by "keep track of which enemy is which"? The Enemy object itself will know what type it itself is.but still somehow be able to keep track of which enemy is which.
- 05-19-2012, 08:02 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Re: Help with inheritance
Ah, so i can make an ArrayList<Enemy> but add a Tank class to it instead of an Enemy class?
- 05-19-2012, 08:05 PM #6
Re: Help with inheritance
You don't add classes; you add instances of classes. If Tank extends Enemy, every Tank is-a Enemy.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-19-2012, 08:27 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Inheritance
By new_2_j in forum New To JavaReplies: 5Last Post: 02-20-2011, 05:56 PM -
JPA Inheritance
By videanuadrian in forum Advanced JavaReplies: 0Last Post: 01-11-2011, 06:30 AM -
Inheritance
By gpio in forum Advanced JavaReplies: 14Last Post: 11-20-2009, 10:41 AM -
Inheritance example
By kris4u4ever in forum New To JavaReplies: 3Last Post: 03-21-2009, 02:53 PM -
Inheritance in GUI
By Marty in forum SWT / JFaceReplies: 2Last Post: 05-11-2007, 12:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks