Implementing an interface with generic type methods

M

Mize-ze

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.
 
E

Eelco

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
 
I

Ingo R. Homann

Hi,

Mize-ze said:
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
 
M

Mark Jeffcoat

Mize-ze said:
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.
 
M

Mize-ze

Ingo,
Excactly what I wanted

Thanks


Hi,



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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top