I have a generic class with this definition:
public class AcoProblemSolver<C, E extends Environment, A extends AntColony<E, Ant<C, E>>> {
Where AntColony goes this way:
public abstract class AntColony<E extends Environment, A extends Ant<?, E>> {
And Ant goes like this:
public abstract class Ant<C, E extends Environment> {
I was hoping to extend AntColony in this fashion:
public class FlowShopProblemSolver extends
AcoProblemSolver<Integer, FlowShopEnvironment, FlowShopAntColony> {
But Eclipse is showing an error on the FlowShopAntColony parameter class:
Bound mismatch: The type FlowShopAntColony is not a valid substitute for the bounded parameter <A extends AntColony<E,Ant<C,E>>> of the type AcoProblemSolver<C,E,A>
Which confuses me, since FlowShopAntColony is defined this way:
public class FlowShopAntColony extends
AntColony<FlowShopEnvironment, AntForFlowShop> {
And AntForFlowShop goes like this:
public class AntForFlowShop extends Ant<Integer, FlowShopEnvironment> {
Why isn't FlowShopAntColony accepted as a valid parameter?
A extends AntColony<E, Ant<C, E>>
The third parameter of AcoProblemSolver has the restriction extends AntColony<E, Ant<C, E>>. The second parameter of AntColony must be exactly Ant<C, E> and you're trying to pass a subclass of Ant. Try:
A extends AntColony<E, ? extends Ant<C, E>>
You may want other similar ? extends clauses elsewhere.
Related
I am creating an interface and an implementation of linked list like so in Java 1.8:
public interface MyList<E extends Comparable<E>> {
.....
}
public class MyListImpl<E> implements MyList<E extends Comparable<E>>{
......
}
The interface has no compiler issues but the MyListImpl is giving an error Unexpected Bound where I have E extends Comparable<E>>. I am not sure why this error is happening though.
The bounds always go on the declaration of the generic type. That's the one at the end of the class name. The one in the implements clause is using the already declared type. So: public class MyListImpl<E extends Comparable<E>> implements MyList<E>.
If you want to make it a bit better, use <E extends Comparable<? super E>>. That looks the same but isn't; with just Comparable<E> you won't be able to support classes like java.sql.Timestamp, which extends java.util.Date which implements Comparable<Date>. In other words: Timestamp is not Comparable<Timestamp> but Comparable<Date>, and using Comparable<? super E> will allow you to use it.
Is it possible to do this in java without problems?
My IDE highlights my code as "Raw use of parameterized class 'Etat' " and "Unchecked call to 'add(E)' as a member of raw type 'java.util.ArrayList'"
My code :
public abstract class Etat<T extends Transition> {}
public abstract class Transition<E extends Etat> {}
Might you be looking for:
class Etat<E extends Etat<E, T>, T extends Transition<E, T>> {}
class Transition<E extends Etat<E, T>, T extends Transition<E, T>> {}
Then, you can do:
class MonEtat extends Etat<MonEtat, MaTransition> {}
class MaTransition extends Transition<MonEtat, MaTransition> {}
allowing the two types to know each other through their type parameter. For instance, if you declare:
class Etat<E extends Etat<E, T>, T extends Transition<E, T>> {
abstract E apply(T transition);
}
You can then be assured that
MonEtat e = ...;
e = e.apply(new MaTransition()); // compiles, and knows that MonEtat is returned
I'm creating a generic interface and its class implementation. It's a disjoint set. The generic type upper boundary for this class is generic itself:
public class MyDisjointSet<K extends Pair<T, T>> implements IMyDisjointSet<K> {
}
and
public interface IMyDisjointSet<T> {}
but I get compile error Cannot resolve symbol T.
When I add a second parameter to the list of generic types it works compiles:
public class MyDisjointSet<K extends Pair<T, T>, T> implements IMyDisjointSet<K>
Why should we put T here? why not before class name?
Please try
public class MyDisjointSet<K extends Pair<T, T>,T> implements IMyDisjointSet<K> {
I want make my List. But I dont know how to write generic type in java.
public interface myListInt <E extends Comparable<E>>{}
public class myList<E extends myListInt<E>> extends LinkedList{}
When I am doing that, it gives an error.How should ı write.
The exact intent of your code is unclear, but I got an error for the <E> in myListInt<E>.
public interface myListInt <E extends Comparable<E>>{}
public class myList<E extends myListInt<E>> extends LinkedList{}
^ Error here
This is because you need to constrain E to extend Comparable<E> in order to be a valid bound for myListInt<E>:
public class myList<E extends Comparable<E> & myListInt<E>>
extends LinkedList{}
However, you maybe also want to add a constraint to LinkedList too (assuming this is java.util.LinkedList:
public class myList<E extends Comparable<E> & myListInt<E>>
extends LinkedList<E> {}
I am trying to make the following work:
import java.io.Serializable;
public abstract class Thing implements Iterable<? extends Serializable> {}
But I compile and get the following error:
Thing.java:2: unexpected type
found : ? extends java.io.Serializable
required: class or interface without bounds
public abstract class Thing extends Iterable<? extends Serializable> {}
I'm not sure why there is ambiguity here? Shouldn't the compiler know what I am intending? I'd love to be able to get this exact functionality if possible, and to understand why this doesn't work.
Thanks for the help!
I assume your intent is to have something like the following:
public class FooBar implements Iterable<? extends Serializable> {
public Iterator<? extends Serializable> iterator() {
return new Itr();
}
}
However, this is functionally equivalent to having the generic parameter be just Serializable. Calling the next method on an Iterator<? extends Serializable> returns a Serializable.
java.lang.Iterable class definition
Iterable<T>
So it will always expect any class or interface to assign to genric parameter T
But you passed a bound type parameter which caused the error.