Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Implementing an interface with generic type methods

Thread view: 
Mize-ze - 13 Nov 2006 11:07 GMT
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.
Eelco - 13 Nov 2006 11:42 GMT
You can do it like this:

> public interface MyInterface
> {
[quoted text clipped - 11 lines]
>   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 - 13 Nov 2006 12:03 GMT
Thanks Eelco,
I Guess that will work but what the point of doing it?

> You can do it like this:
>
[quoted text clipped - 21 lines]
> Best regards,
> Eelco
Mark Jeffcoat - 13 Nov 2006 15:08 GMT
> 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.

Signature

Mark Jeffcoat
Austin, TX

Ingo R. Homann - 13 Nov 2006 12:22 GMT
Hi,

> An example of what I want will make it clearer:
>
[quoted text clipped - 15 lines]
>   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 - 16 Nov 2006 13:02 GMT
Ingo,
Excactly what I wanted

Thanks

> Hi,
>
[quoted text clipped - 39 lines]
> Ciao,
> Ingo


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.