This is one of the pillars of object oriented programming, polymorphism.
Basically, you need to define an interface with the methods you need. Note that in java, interface methods are public by default.
public interface Renderable {
void render();
void update();
}
And then you define the implementation. To implement an interface you need to use the "implements" key word. In the example below, you'll see "implements Renderable"
public class MyRenderable implements Renderable {
public void render() {
... // method impl
}
public void update() {
... // method impl
}
}
And finally, you create an instance and call the methods through the interface.
Renderable r = new MyRenderable();
r.render();
r.update();
From here you can populate a list with the type being that of your interface. You can iterate over that list, calling the methods from the interface, invoking the implementations.
interfaceOr anabstract classif you want a default implementation. Or twointerfaces with adefaultmethod if you use Java 8.#updateon every instance of an object of a class that implements that interface?