I have an ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
. When I try to iterate it, I get:
Incopatible types:
Required: java.lang.Class <? extends IMyInterface>
Found: IMyInterface
My iteration
for (IMyInterface iMyInterface : IMyInterface.getMyPluggables()) {}
Red code warning highlight (Android Studio)
Error:(35, 90) error: incompatible types: Class<? extends IMyInterface> cannot be converted to IMyInterface
I would like to
ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
for (Class<? extends IMyInterface> myClass : classes) {
if (myClass instanceof IMyInterface) {
View revPluggableViewLL = myClass.getMyInterfaceMethod();
}
}
ERROR
Inconvertible types; cannot cast 'java.lang.Class<capture<? extends com.myapp.IMyInterface>>' to 'com.myapp.IMyInterface'
How can I go about iterating through it?
Thank you all in advance.
You want to iterate on instances of
IMyInterface
as you want to invoke a specific method ofIMyInterface
:The problem is that you declared a
List
ofClass
instances :It doesn't contain any instance of
IMyInterface
but onlyClass
instances.To achieve your need, declare a list of
IMyInterface
:And use it in this way :
Note that this check is not required :
You manipulate a
List
ofIMyInterface
, so elements of theList
are necessarily instances ofIMyInterface
.myClass
is an instance ofClass
, which doesn't implementIMyInterface
(even if it'sClass<IMyInterface>
). Therefore you can never executegetMyInterfaceMethod()
onmyClass
.