Implement an interface that extends comparable - java

So I have an interface Foo is constructed like:
public interface Foo<T extends Comparable<? super T>>
and I'm trying to implement Foo in my class bar like:
public class Bar<T> implements Foo<T>
but I get getting the error type argument T#1 is not within bounds of type-variable T#2
but when I try to implement Foo as:
public class Bar<T> implements Foo<T extends Comparable<? super T>>
I get > expected and <identifier> expected

Your syntax is a bit off. Try moving the bounds to the declaration of T as in this:
public class Bar<T extends Comparable<? super T>> implements Foo<T> {
...
}

Related

Java two-way parameterized class

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

Getting compile error after extending Generic to Comparable<Generic>

class A<T extends Comparable<? super T>> {
Supplier<T> supp;
A(Supplier<T> supp) {
this.supp = supp;
}
static <T extends Comparable<? super T>> A<T> of(Supplier<T> supp) {
return new A<T>(supplier);
}
<U> A<U> map(Function<? super T, ? extends U> mapper) {
return A.of(() -> mapper.apply(this.supp.get()));
}
}
When I compile, I get error saying:
type argument U is not within bounds of type-variable T
where U,T are type-variables:
U extends Object declared in method <U>map(Function<? super T,? extends U>)
T extends Comparable<? super T> declared in class A
I feel that I need to make U in mapper extend something, but I am not really sure how, I have tried U extends Comparable, etc. but they don't work.

The generic type upper bound to be generic itself

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> {

Java wildcards with super

I read that using a wildcard with super like this:
public class MyClass <T extends Comparable<? super T>> {
...
}
instead of:
public class MyClass <T extends Comparable<T>> {
...
}
could make the class 'more generic', but I do not understand why.
Can someone provide some concrete examples?
This way you can supply a class for T, which does not for itself implements Comparable, but inherits from a class implementing Comparable.
E.g.
class Baseclass implements Comparable<Baseclass> {
...
}
class Inherited extends Baseclass {
...
}
With a specification like
public class MyClass <T extends Comparable<? super T>> {
...
}
you can use MyClass<Inherited>, and MyClass<Baseclass>, but with
public class MyClass <T extends Comparable<T>> {
...
}
you can only use MyClass<Baseclass>

declaring Iterator<? extends E & Comparable<? super E>> iterator in java

Can I declare the following in Java?
public class NewIterator<E extends Comparable<? super E>> implements Iterator<E> {
NewIterator(Iterator<? extends E & Comparable<? super E>> iterator){
...
}
I am getting an error saying
Multiple markers at this line
- Incorrect number of arguments for type Iterator<E>; it cannot be parameterized with arguments <? extends E, Comparable<? super E>>
- Syntax error on token ",", ; expected
- Syntax error on token "&", , expected
- Syntax error on token ")", ; expected
By defining your class as
class NewIterator<E extends Comparable<? super E>> implements Iterator<E> {
you say that E has to implement Comparable<? super E>.
Now in the constructor you try to repeat that and allow subtypes of E.
NewIterator(Iterator<? extends E & Comparable<? super E>> iterator){
...
}
If you do just
public NewIterator(Iterator<? extends E> iterator) {
}
You should get what you want because E already defines that it's a type that implements the comparable interface.
Example
class IntegerNumber {}
class PositiveNumber extends IntegerNumber implements Comparable<IntegerNumber> {}
class OddPositiveNumber extends PositiveNumber {}
private NewIterator<PositiveNumber> newIterator;
void foo() {
Iterator<PositiveNumber> iterator = createIteratorFrom(
new PositiveNumber(1),
new OddPositiveNumber(7)
);
this.newIterator = new NewIterator(iterator);
}
If you use PositiveNumber in NewIterator<E extends Comparable<? super E>> you can replace E by PositiveNumber. So your constructor accepts Iterator<? extends PositiveNumber>. You can now create an iterator over any subclass of PositiveNumber but since that class inherits from PositiveNumber it must also inherit the Comparable<IntegerNumber> interface.

Categories

Resources