To go around having to implement ALL the methods in an interface, I created an abstract class that implements an interface, then have other classes extend the abstract class and override only the needed methods.
I am building an API / Framework for my app.
I would like to add classes that are instances of an interface IMyInterface
to an ArrayList
:
ArrayList<Class<IMyInterface>> classes = new ArrayList<>();
classes.add(MyClass.class);
Here is MyClass
class MyClass extends AbstractIMyInterface {}
Here is AbstractIMyInterface
class AbstractIMyInterface implements IMyInterface {}
So far this seems impossible. My approach above won't work:
add (java.lang.Class<com.app.IMyInterface>)
in ArrayList cannot be applied to
(java.lang.Class<com.myapp.plugin.plugin_a.MyClass>)
How can I make this work, ie: Add a class extending another class to an ArrayList
you need to use wildcard ? extends IMyInterface
.
ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
In ArrayList<Class<IMyInterface>> classes
, you can only add Class<IMyInterface>
.
You can use ?
for that.
List<Class <? extends IMyInterface>> arrayList = new ArrayList<>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With