Java wildcards with super - java

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>

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

How to extend Node<T extends Comparable<T>> class

I have an abstract class that is like this:
public abstract class Node<T extends Comparable<T>>
I need extends Comparable<T> so that I may use .compareTo() with generic type T.
I try to extend this class with this sub-class declaration:
public class ListNode<T> extends Node<T extends Comparable<T>>
However this throws SyntaxErrors in eclipse for the second extends and for the final two >>.
Is this the way to declare the sub-class? It seems so messy.
public class ListNode<T extends Comparable<T>> extends Node<T extends Comparable<T>>
How should this be done? Cheers in advance.
With Node as
public abstract class Node<T extends Comparable<T>>{}
ListNode can be:
public class ListNode<T extends Comparable<T>> extends Node<T>
The key thing to note is that the type parameter after the name of the declared class is what constrains T for everything else in the class. This includes the other types extended by the declared class.
This means that once you have bounded T you don't need to do it again in the extends/implements statements.

Implementing bounded generic with java

I have an interface
public interface TransferObjectUtil<B extends BusinessObject,T> {
public T to(B domain);
public B from(T transferObject);
}
I am implementing the class for this as
public class ReflectionBasedTransferObjectUtil<B extends BusinessObject, T> implements
**TransferObjectUtil<B extends BusinessObject, T>** {
For the portion within the ** in the above line the compiler complains that for B extends BusinessObject is not allowed. Why would it be so ?
I am just starting with generics, so pardon me for my novice question.
Your location for the generics is a bit wrong / overeager, try it like this
public class ReflectionBasedTransferObjectUtil<B extends BusinessObject, T> implements TransferObjectUtil<B, T> {

Java Bound Mismatch in recursive generics/inheritance

I have the following structure:
public abstract class A <E extends El, U extends A<E,U> > { ... }
public class B<E extends El> extends A<E, B<E> > { ... }
public abstract class C <E extends El, T extends A<E, T>> { ... }
My question is, why can I do this:
public class R extends C<El, B<El>> { ... }
but not
public class R <T extends B<El>> extends C<El, T> { ... }
Why is T (which extends B<El>) not a good substitute for B<El>?
The exception which I get is
Bound mismatch: The type T is not a valid substitute for the bounded parameter <T extends A<El,T>> of the type C<E,T>
Try to declare A and C as follows
public abstract class A <E extends El, U extends A<E, ? super U>> {}
public abstract class C <E extends El, T extends A<E, ? super T>> {}
My question is, why can I do this:
public class R extends C<El, B<El>> { ... }
Because El extends El, and B<El> extends A<El, B<El>>
but not
public class R<T extends B<El>> extends C<El, T> { ... }
Because T does not extend A<El, T>. We know that T extends B<El>, and that B<El> extends A<El, B<El>>, and not A<El, T>. This declaration is in general unsafe.
It's impossible to give any suggestions without knowing what you do with these types inside the classes, and why you think the declaration is safe. For example, if it is known that A only serves as a "consumer" of U, then you can use a super bound and it will work:
public abstract class A<E extends El, U extends A<E, ? super U>> { ... }
public abstract class C<E extends El, T extends A<E, ? super T>> { ... }
abstract class A <E extends El, U extends A<E, U> > {
}
class B<E extends El> extends A<E, B<E> > {
}
abstract class C <E extends El, T extends A<E, ? super T>> {
public void X(T t, El a) {
}
}
class R <T extends B<El>> extends C<El, T> {
}

Implement an interface that extends comparable

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

Categories

Resources