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.
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.
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 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.
My main question revolves around when to use and what is the difference between the following when combined with Class, Abstract, Interface:
<E>
<E extends Interface>
<? extends Interface>
Shown below is a detailed question with some code signatures:
This code uses Guava Forwarding Decorators to define specific collections.
Base Interface:
public interface AnimalSetInterface<E extends AnimalI> extends Set<E>
This works:
public interface AsiaI<E extends AnimalI> extends AnimalSetInterface<E>
The following gives an error:
public interface AsiaI<E> extends AnimalSetInterface<E>
Bound mismatch: The type E is not a valid substitute for the bounded
parameter of the type AnimalSetInterface
What I am trying to understand is if I have specified at the Base Interface that I only want <E extends AnimalI> then why do I have to specify again in AsiaI?
I am trying to understand generics and at the same time minimize code.
Also if both classes have such code is there a good way to combine/minimize (remove/generify boilerplate code) it:
Asia:
public Asia(final ImmutableSet<E> animalSet){
super(animalSet);
}
public static <E extends AnimalI> AsiaI<E> of(final ImmutableSet<E> animalSet){
return new Asia(animalSet);
}
Africa:
public Africa(final ImmutableSet<E> animalSet){
super(animalSet);
}
public static <E extends AnimalI> AfricaI<E> of(final ImmutableSet<E> animalSet){
return new Africa(animalSet);
}
public class Africa<E extends AnimalI> extends AnimalSetAbstract implements AfricaI
public class Asia<E> extends AnimalSetAbstract implements AsiaI
The difference is that in the first case, your generic type must extend AnimalI
In the second case, your generic type could be any class.