Implementing an interface with generic type methods

Discussion in 'Java' started by Mize-ze, Nov 13, 2006.

  1. Mize-ze

    Mize-ze Guest

    I want to create an interface with two methods

    The classes that will implement this interface will sometimes need the
    methods with different arguments and return types. how can this be
    done? what is the simpliest solution? I realize that it can be two
    different interfaces but that is really not good with my design.

    An example of what I want will make it clearer:

    public interface MyInterface
    {
    public ??? foo(??? arg)
    public ??? bar(??? arg)
    }

    public classA implements MyInterface
    {
    public String foo(String arg);
    public String bar(String arg);
    }

    public classB implements MyInterface
    {
    public int foo(Object arg);
    public String bar(Object arg);
    }


    Thanks.
     
    Mize-ze, Nov 13, 2006
    #1
    1. Advertising

  2. Mize-ze

    Eelco Guest

    You can do it like this:

    > public interface MyInterface
    > {
    > }
    >
    > public classA implements MyInterface
    > {
    > public String foo(String arg);
    > public String bar(String arg);
    > }
    >
    > public classB implements MyInterface
    > {
    > public int foo(Object arg);
    > public String bar(Object arg);
    > }


    This way you have a common interface with no abstract methods
    specified.

    Hope this helps.

    Best regards,
    Eelco
     
    Eelco, Nov 13, 2006
    #2
    1. Advertising

  3. Mize-ze

    Mize-ze Guest

    Thanks Eelco,
    I Guess that will work but what the point of doing it?


    Eelco wrote:
    > You can do it like this:
    >
    > > public interface MyInterface
    > > {
    > > }
    > >
    > > public classA implements MyInterface
    > > {
    > > public String foo(String arg);
    > > public String bar(String arg);
    > > }
    > >
    > > public classB implements MyInterface
    > > {
    > > public int foo(Object arg);
    > > public String bar(Object arg);
    > > }

    >
    > This way you have a common interface with no abstract methods
    > specified.
    >
    > Hope this helps.
    >
    > Best regards,
    > Eelco
     
    Mize-ze, Nov 13, 2006
    #3
  4. Hi,

    Mize-ze wrote:
    > An example of what I want will make it clearer:
    >
    > public interface MyInterface
    > {
    > public ??? foo(??? arg)
    > public ??? bar(??? arg)
    > }
    >
    > public classA implements MyInterface
    > {
    > public String foo(String arg);
    > public String bar(String arg);
    > }
    >
    > public classB implements MyInterface
    > {
    > public int foo(Object arg);
    > public String bar(Object arg);
    > }


    Whats wrong with:

    public interface MyInterface<FooParam,FooReturn,BarParam,BarReturn> {
    public FooReturn foo(FooParam arg);
    public BarReturn bar(BarParam arg);
    }

    public classA implements MyInterface<String,String,String,String>
    {
    public String foo(String arg){...}
    public String bar(String arg){...}
    }

    public classB implements MyInterface<Object,Integer,Object,String>
    {
    public Integer foo(Object arg){...}
    public String bar(Object arg){...}
    }

    Ciao,
    Ingo
     
    Ingo R. Homann, Nov 13, 2006
    #4
  5. "Mize-ze" <> writes:

    > Thanks Eelco,
    > I Guess that will work but what the point of doing it?
    >
    >


    Please don't top post.


    You have to tell us what the point of doing it is if
    you want a useful answer.

    Pending that, you may have set up a situation that Java's
    type system doesn't handle particularly gracefully, and
    you'll have to define the interface's methods to accept
    and return Object. Most of the time I see that, though,
    there's a better way--more idiomatic, at least.


    --
    Mark Jeffcoat
    Austin, TX
     
    Mark Jeffcoat, Nov 13, 2006
    #5
  6. Mize-ze

    Mize-ze Guest

    Ingo,
    Excactly what I wanted

    Thanks



    Ingo R. Homann wrote:
    > Hi,
    >
    > Mize-ze wrote:
    > > An example of what I want will make it clearer:
    > >
    > > public interface MyInterface
    > > {
    > > public ??? foo(??? arg)
    > > public ??? bar(??? arg)
    > > }
    > >
    > > public classA implements MyInterface
    > > {
    > > public String foo(String arg);
    > > public String bar(String arg);
    > > }
    > >
    > > public classB implements MyInterface
    > > {
    > > public int foo(Object arg);
    > > public String bar(Object arg);
    > > }

    >
    > Whats wrong with:
    >
    > public interface MyInterface<FooParam,FooReturn,BarParam,BarReturn> {
    > public FooReturn foo(FooParam arg);
    > public BarReturn bar(BarParam arg);
    > }
    >
    > public classA implements MyInterface<String,String,String,String>
    > {
    > public String foo(String arg){...}
    > public String bar(String arg){...}
    > }
    >
    > public classB implements MyInterface<Object,Integer,Object,String>
    > {
    > public Integer foo(Object arg){...}
    > public String bar(Object arg){...}
    > }
    >
    > Ciao,
    > Ingo
     
    Mize-ze, Nov 16, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,053
    Chaitanya
    Feb 3, 2009
  2. z-man
    Replies:
    2
    Views:
    452
    z-man
    Oct 1, 2006
  3. tshad
    Replies:
    3
    Views:
    425
    Barrie Wilson
    Dec 7, 2007
  4. minlearn
    Replies:
    2
    Views:
    459
    red floyd
    Mar 13, 2009
  5. Kenneth McDonald
    Replies:
    5
    Views:
    326
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page