Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Implementing an interface with generic type methods

Reply
Thread Tools

Implementing an interface with generic type methods

 
 
Mize-ze
Guest
Posts: n/a
 
      11-13-2006

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.

 
Reply With Quote
 
 
 
 
Eelco
Guest
Posts: n/a
 
      11-13-2006
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

 
Reply With Quote
 
 
 
 
Mize-ze
Guest
Posts: n/a
 
      11-13-2006
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


 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      11-13-2006
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

 
Reply With Quote
 
Mark Jeffcoat
Guest
Posts: n/a
 
      11-13-2006
"Mize-ze" <(E-Mail Removed)> 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
 
Reply With Quote
 
Mize-ze
Guest
Posts: n/a
 
      11-16-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Interface called without implementing all methods tshad ASP .Net 3 12-07-2007 04:57 PM
Generic methods: how to express explicit type parameters? z-man Java 2 10-01-2006 10:54 PM



Advertisments