interface Parent<T extends Parent<T>> extends Iterable<T> {
        }

        interface Child<T extends Child<T>> extends Parent<T> {
        }

        class ParentImpl implements Parent<ParentImpl> {
           public Iterator<ParentImpl> iterator() {
               List<ParentImpl> list = new ArrayList<ParentImpl>();
               list.add(new ParentImpl());
               return list.iterator();
           }
        }

        class ChildImpl implements Child<ChildImpl> {
           public Iterator<ChildImpl> iterator() {
               List<ChildImpl> list = new ArrayList<ChildImpl>();
               list.add(new ChildImpl());
               return list.iterator();
           }
        }

        public class Tester {
           public static void main(String[] args) {
               Parent<ParentImpl> parent = new ParentImpl();
               for (ParentImpl p2 : parent) {
                   System.out.println("> " + p2);
                   for (Parent<ParentImpl> p3 : p2) {
                       System.out.println(">> " + p3);
                   }
               }

               ParentImpl p = parent.iterator().next();

               ChildImpl child = new ChildImpl();
               for (ChildImpl c2 : child) {
                   System.out.println("> " + c2);
                   for (ChildImpl c3 : c2) {
                       System.out.println(">> " + c3);
                   }
               }

               ChildImpl c = child.iterator().next();
           }
        }

On Jan 28, 10:11 pm, Christian Catchpole <christ...@catchpole.net>
wrote:
> My brain hurts.  I love Iterable,  In this example, I'm trying to
> define a Parent interface which iterates a sub-list of it's self
> type.  But I want to be able to inherit to Child and change the
> Iterator to Child.  Which in theory should work because Child is a sub
> class of Parent.  I would expect any code which can operate on Child,
> be able to operate on Parent.
>
> My IDE thinks the "for (Parent p2 : parent)" loops are valid, but they
> don't compile.  I think the problem is I'm not actually implementing
> Parent with any Generic type info.. but I can't find any type
> combinations which work.  And I'd prefer to not have the implementor
> need to pass the type info.
>
> interface Parent<T extends Parent<T>> extends Iterable<T> {
>
> }
>
> interface Child<T extends Child<T>> extends Parent<T> {
>
> }
>
> class ParentImpl implements Parent {
>     public Iterator<Parent> iterator() {
>         List<Parent> list = new ArrayList<Parent>();
>         list.add(new ParentImpl());
>         return list.iterator();
>     }
>
> }
>
> class ChildImpl implements Child {
>     public Iterator<Child> iterator() {
>         List<Child> list = new ArrayList<Child>();
>         list.add(new ChildImpl());
>         return list.iterator();
>     }
>
> }
>
> public class Tester {
>     public static void main(String[] args) {
>         Parent parent = new ParentImpl();
>         for (Parent p2 : parent) {
>             System.out.println("> " + p2);
>             for (Parent p3 : p2) {
>                 System.out.println(">> " + p3);
>             }
>         }
>
>         Parent p = parent.iterator().next();
>
>         Child child = new ChildImpl();
>         for (Child c2 : child) {
>             System.out.println("> " + c2);
>             for (Child c3 : c2) {
>                 System.out.println(">> " + c3);
>             }
>         }
>
>         Child c = child.iterator().next();
>     }
>
> }
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to javaposse@googlegroups.com
To unsubscribe from this group, send email to 
javaposse+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to