5 Replies Latest reply: Aug 6, 2011 2:21 AM by YoungWinston RSS

    Generics Interface With 2 Types?

    809103
      I folks I'm trying to define an interface with 2 types, something like:

      public MyInterface <E, T>{
      public T init(E);
      public void doStuff(T);
      }

      now in my implementation classes I would like:

      abstract public class <Foo, Bar> AbstractBaseClass implements MyInterface<Foo,Bar> {

      }

      ..and finally
      public class <Foo,SubBar> exends AbstractBaseClass<Foo,SubBar>{
      }

      where SubBar extends Bar

      I've tried ? extends Bar, and ? extends T etc..but no luck..

      any pointers? is this doable?
        • 1. Re: Generics Interface With 2 Types?
          EJP
          Hard to be sure but something like this, after fixing all your syntax errors:
          class Foo
          {
          }
          
          class Bar
          {
          }
          
          interface MyInterface<E, T>
          {
               T init(E e);
               void doStuff(T t);
          }
          
          abstract class AbstractBaseClass<E, T> implements MyInterface<E, T>
          {
          }
          
          class MyClass<T extends Bar> extends AbstractBaseClass<Foo, T>
          {
               public T     init(E t){ return null; }
               public     void doStuff(T t){}
          }
          • 2. Re: Generics Interface With 2 Types?
            maheshguruswamy
            806100 wrote:
            I folks I'm trying to define an interface with 2 types, something like:

            public MyInterface <E, T>{
            public T init(E);
            public void doStuff(T);
            }

            now in my implementation classes I would like:

            abstract public class <Foo, Bar> AbstractBaseClass implements MyInterface<Foo,Bar> {

            }

            ..and finally
            public class <Foo,SubBar> exends AbstractBaseClass<Foo,SubBar>{
            }

            where SubBar extends Bar

            I've tried ? extends Bar, and ? extends T etc..but no luck..

            any pointers? is this doable?
            Its doable. Your syntax for adding types to classes is incorrect. For e.g. your AbstractBaseClass should look like this
            public abstract class AbstractBaseClass<E,T> implements MyInterface<E, T> {
            
            }
            • 3. Re: Generics Interface With 2 Types?
              YoungWinston
              806100 wrote:
              I folks I'm trying to define an interface with 2 types, something like:...
              Like EJP, I'm not exactly sure what you're driving at, but for your specific example you could use:
              public abstract class AbstractBaseClass<T extends Bar>
                 implements MyInterface<Foo,T> {...
              and then
              public class SubClass
                 extends AbstractBaseClass<SubBar>{...
              for your subclass.

              If however you need to ringfence the types for your interface, you could do something like:
              public MyInterface <E extends Foo, T extends Bar>{...
              but only you can decide which is best.

              Winston
              • 4. Re: Generics Interface With 2 Types?
                jschellSomeoneStoleMyAlias
                Just noting there is a generics forum.
                • 5. Re: Generics Interface With 2 Types?
                  YoungWinston
                  jschell wrote:
                  Just noting there is a generics forum.
                  Ah. Didn't know that. Thanks.

                  Winston