I'm currently not sure how inheritance would best fit into my Java program, or whether it would best be implemented using interfaces or not. An example of the context I'm talking about is below.
Imagine that I'm making a game, and I have enemies in my game. These enemies all have various variables and behaviours in common, but are all unique in some way. For example, Enemy1,
class Enemy1 {
float x, y, width, height;
public Component(float x, float y, float width, float height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void AI() {
//some code
}
}
and Enemy2,
class Enemy2 {
float x, y, width, height;
public Component(float x, float y, float width, float height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void AI() {
//some different code
}
}
So with these two separate enemies, I understand (or at least believe) that I can change Enemy2 to resemble
class Enemy2 extends Enemy1 {
@Override
public void AI() {
//some different code
}
}
But, what now if I have five different types of AI code each enemy could have, and then instead of defining the code for the AI behaviour within the class for each enemy, I want to have another class which contains all of the AIs, allowing me to extend/implement the AI class/interface and select the one I want.
I'm not sure what the best way to do any of this is. In a nutshell, I want to have different enemies which share properties, each which would have similar, but different, functions within them (such as the AI code), but overall would still be quite unique. Would it be most efficient to do this with only classes? Interfaces? And how would I go about doing this? Would I have a class drawing off a class drawing off a class? Or just a class extending a class and implementing an interface at the same time? I'm not quite sure what the best inheritance structure would be to use.
Sorry if I have made any fundamental misunderstandings or mistakes.
Any help would be much appreciated.