This discussion is archived
3 Replies Latest reply: Mar 29, 2010 2:09 AM by 843793 RSS

collection/iterator of inner class as return type of method in outer class

843793 Newbie
Currently Being Moderated
Hello. I am not sure whether this can be done or not. If so, I cannot figure out how. I have a family of classes and I would like there to be a uniform way for an instance of any member of the family to show its contents to a caller. I would also like the caller to be able to treat each member individually rather than treating them all as one superclass.
Something like this:
class X1
{
   ...
   class Y1 {...}
   ...
   Collection<Y1> getStuffAllAtOnce() {...}
   Iterator<Y1> getStuffOneByOne() {...}
   ...
}
class X2 extends X1
{
   ...
   class Y2 extends Y1 {...}
   ...
   Collection<Y2> getStuffAllAtOnce() {...}
   Iterator<Y2> getStuffOneByOne() {...}
   ...
}
When I write it like this I get compilation errors. I understand that Collection<Y2> is not a subclass of Collection<Y1> but supposedly it will be treated as such if the compiler is satisfied that the collection will not be modified. But how does the compiler decide and how can it be convinced? Thanks. Eli