Array of Interface in Java Array of Interface in Java arrays arrays

Array of Interface in Java


yes, it is possible. You need to fill the fields of the array with objects of Type Module

instances[0] = new MyModule();

And MyModule is a class implementing the Module interface. Alternatively you could use anonymous inner classes:

instances[0] = new Module() { public void actions() {} public void init() {}};

Does this answer your question?


You would need to fill the array with instances of a class(es) that implement that interface.

Module[] instances = new Module[20];for (int i = 0; i < 20; i++){    instances[i] = new myClassThatImplementsModule();}


You need to create a concrete class type that would implement that interface and use that in your array creation