I am looking for help with having multiple different types of system accessed within java as if they are the same.
For example, I have the classes
private class SystemA{
public void in(boolean input){
//do x;
}
public boolean out(){
//return x;
}
}
Say I want to have an ArrayList of different systems. These systems all implement the functions in and out like SystemA does, but they will be different objects all with different internal architectures.
I would like to for example iterate through the aformentioned arraylist, calling out() on all of the objects – how can I:
Store multiple different object types.
Ensure that I can call the in and out functions on the object in the arraylist, not needing to worry that the object is one specific type or another.
Let your system classes implement a new
MySysteminterface:E.g.:
Your
ArrayListwill then look like any of these:I suggest reading the Java tutorial “What Is an Interface?” for more details. Also, read up on “subtype polymorphism” in general.