2

I realize the issue of generic type conflicts has been successfully confronted many times over on this site, and the advice given has helped me in the past, but in this case, I cannot seem to determine the source of the conflict.

The first declaration in my Graph class is giving me an error:

error: type argument Vertex<T#1> is not within bounds of type-variable T#2
    private AVLTree<Vertex<T>> vertices;
                          ^
  where T#1,T#2 are type-variables:
    T#1 extends Comparable<T#1> declared in class Graph
    T#2 extends Comparable<T#2> declared in class AVLTree

Here is the Graph class declaration and the problematic field:

public class Graph<T extends Comparable<T>> implements GraphInterface<T> {

    private AVLTree<Vertex<T>> vertices;

    ...

    // implemented methods, etc.

    ...

}

AVLTree class declaration:

public class AVLTree<T extends Comparable<T>> { ... }

And the Vertex class declaration:

public class Vertex<T extends Comparable<T>> implements Comparable<Vertex> { ... }

I don't see what's wrong here. Any ideas?

K1nesthesia
  • 147
  • 1
  • 3
  • 11

1 Answers1

4

All you have to do is make Vertex implement Comparable<Vertex<T>>:

public class Vertex<T extends Comparable<T>> implements Comparable<Vertex<T>> { ... }

You can do a bit of "substitution" to figure out why:

  • AVLTree has the bounded type parameter T extends Comparable<T>.

  • In the type AVLTree<Vertex<T>>, Vertex<T> takes the place of T.

  • AVLTree requires T to extend Comparable<T>, so if we substitute: Vertex<T> is required to extend Comparable<Vertex<T>>

August
  • 12,410
  • 3
  • 35
  • 51
  • Thanks, it's working perfectly now! I remember I had tried that after several stabs in the dark. It seems it takes a few seconds for my IDE to acknowledge the change and remove the flag. In my frustration I undid the correct solution before it had the chance the work. Lesson learned: BE PATIENT ;) – K1nesthesia Dec 09 '14 at 03:52