Of course, you can create an array whose type is an interface. You just need to put references to specific instances of this interface in an array created with a name or anonymously before using elements in it. The following is a simple example that displays the hash code of an array object. If you try to use any element, say myArray [0] .method1 (), you will get NPE.
public class Test { public static void main(String[] args) { MyInterface[] myArray = new MyInterface[10]; System.out.println(myArray); } public interface MyInterface { void method1(); void method2(); } }
michaelliu
source share