I’ve read similar questions here but I’m still a little confused.
MyCollection extends ArrayList<MyClass>
MyClass implements Data
yet this gives me the “cannot convert from ArrayList to MyCollection”
MyCollection mycollection = somehandler.getCollection();
where getCollection looks like this
public ArrayList<Data> getCollection()
So my assumptions are obviously wrong. How can I make this work like I would like it to
ArrayListdoes not extendMyCollection. It’s the other way round.There are several ways to fix this problem, depending on the functional requirement:
MyCollectionconstructor which can take anotherCollection.MyCollection, then you need to castArrayListtoMyCollection.MyCollection, then you need to change return type toMyCollection.That said, what makes
MyCollectionso different that it apparently doesn’t adhere theList‘s contract and you hence need to cast/convert it? This shouldn’t happen, I would rethink the design/approach.