Generics: multiple interfaces as return parameter does not work

M

martinus

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
 
L

Lew

martinus said:
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?

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

Daniel Pitts

martinus said:
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
There was a discussion about this some time ago.
Also, I've written an article about this.

<http://virtualinfinity.net/wordpres...java-or-interest-in-interfaces-is-invaluable/>
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top