Search code examples
javagenericscomparable

What mean this expression in Generic


Can someone please explain to me what this expression in java means:

class BinaryNode<AnyType extends Comparable<? super AnyType>>

What does "AnyType extends Comparable" mean?


Solution

  • This declares a generic type parameter called AnyType. The rest of the declaration, extends Comparable<? super AnyType>, places an upper bound on what AnyType can be. Specifically, whatever AnyType is must be Comparable, and Comparable's type argument can be what AnyType is, or anything that is a superclass of that type. E.g. it could be Integer, because Integer is Comparable<Integer>. However, it could be some class that is Comparable<Object>, because Object is the superclass to all object types.