I have several objects of different types which had to be stored in the same arrayList. I did this because most classes share similar functions.
ArrayList <Node> objectArray = new ArrayList();
With an interface
interface Node {
void assignID(int ID);
void Draw();
int getColumn();
int getRow();
void setGridSize(int _gridSize);
void turnLeft();
void turnRight();
void setPos(int Xpos, int Ypos);
boolean getState();
void setState(boolean state);
int getID();
void addSwitch(int switchID, boolean state);
void triggered();
}
I can do the following:
for (int i = 0; i < objectArray.size(); i++) {
Node anyClass = objectArray.get(i);
if(column == anyClass.getColumn() && row == anyClass.getRow()) {
index = i;
I can acces all members of any object of choise by getting the X and Y positions.
The problem I have with this is the following: In order to use a function of a certain object using the Node object. The function in question needs to be in the interface. If I add a function to the interface. All other classed need to have this function. That means that if i want to add one specific function to one class. I am forced to add dummy functions to all other classes as well. At first it was no problem but now my program grows it is getting annoying to keep making dummy functions.
How can I best work arround this? My only two demands are that I keep my arrayList with different object types and that I give certain objects unique functions. Do I have to something with inheritance??