I have a problem with returning generic collection from getter method. My class looks like this:
public abstract class ContentGroup<E extends Content> extends Content {
private List<E> childContents = new LinkedList<E>();
public List<E> getChildContents() {
return childContents;
}
...
}
public class Container extends ContentGroup {
}
When I call getChildContents()
method it returns a list, but not a list of objects extending Content
class so I have to explicitly cast returned value to Content
:
public void doit(Container contentGroup) {
//Why does get method return Object instead of Content?
Content content = (Content) contentGroup.getChildContents().get(0);
...
}
Edit
I updated the code to reflect better the acctual implementation. As one of the answers suggests the problem was that Container did not define Type. Problem was solved with:
public class Container extends ContentGroup<Content> {
}