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 2007

Tip: Looking for answers? Try searching our database.

Generics: multiple interfaces as return parameter does not work

Thread view: 
martinus - 19 Nov 2007 16:02 GMT
Hi all, I have implemented an interface that looks somewhat like this:

public interface LocalOptimizer {
   <X extends List<Node> & RandomAccess> void optimize(final X tour);
   <X extends List<Node> & RandomAccess> X doSomething();
}

The method optimize() requires a class that implements List<Node> and
RandomAccess, for example ArrayList does this. This works great, I can
call it like this:

       optimize(new ArrayList<Node>());

But I cannot use the second method, this code does not compile:

   public <X extends List<Node> & RandomAccess> X doSomething() {
       return new ArrayList<Node>();
   }

I get the error message "Type mismatch: cannot convert from
ArrayList<Node> to X"
Any ideas how I can make this work?

Martin
Lew - 19 Nov 2007 16:39 GMT
> Hi all, I have implemented an interface that looks somewhat like this:
>
[quoted text clipped - 18 lines]
> ArrayList<Node> to X"
> Any ideas how I can make this work?

Generic methods require an argument to allow the compiler to resolve what X is.

A better way might be to make the interface itself generic:

public interface LocalOptimizer< T extends List<Node> & RandomAccess>
{
  void optimize( final T tour );
  T doSomething():
}

public class Foo extends LocalOptimizer<ArrayList<Node>>
{
  public optimize( ArrayList<Node> nodes ) { ... }
  public ArrayList<Node> doSomething(){ ... }
}

You could also drop the generics altogether, at least for doSomething().

public ArrayList<Node> doSomething();

Do you really need the return type to be *any* List that is RandomAccess?  You
could just insist that it be an ArrayList.

I wouldn't suggest using Vector, but perhaps you don't care whether it's an
ArrayList or a Stack.  OTOH, ArrayList and Stack are not usually
interchangeable in an algorithm.  Chances are that the only useful type for
these methods is ArrayList.  If so, you can drop the generics altogether and
just use that.

Signature

Lew

Daniel Pitts - 19 Nov 2007 19:35 GMT
> Hi all, I have implemented an interface that looks somewhat like this:
>
[quoted text clipped - 20 lines]
>
> Martin
There was a discussion about this some time ago.
Also, I've written an article about this.

<http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/03/06/type
-intersection-in-java-or-interest-in-interfaces-is-invaluable/
>

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

martinus - 20 Nov 2007 07:01 GMT
> There was a discussion about this some time ago.
> Also, I've written an article about this.
>
> <http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007...>

Thanks!

--
Martin


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



©2009 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.