I have java Generics related question
I have Parents class called Container
like below: Container can contain other containers and Services
. Services class extends Container but can contain only ServiceMembers
which also extend Container.
The below code doesnt work even though in the Services class I override the parents class's getElements()
method and return an ArrayList of elements which are also Subclass of the Container class.
Any idea on how I can get this to work or I am just using it the wrong way ?
public class Container
{
protected ArrayList<Container> elements = new ArrayList<Container>();
public ArrayList<Container> getElements()
{
}
}
public class Service extends Container
{
public ArrayList<ServiceMember> getElements()
{
}
}