Search code examples
javamethodsgeneric-programming

Type E recognized as an Object


why if I write this:

Node<Vertex<E>, Double> a = queue.extractMax();
Vertex<E> u = a.getValue();

The code compile without errors and if I write this:

Vertex<E> u = queue.extractMax().getValue();

I got the error:

error: incompatible types: Object cannot be converted to Vertex


Solution

  • Assuming your extractMax() signature/return type is different from Node<Vertex<E>, Double> -

    Vertex<E> u = ((Node<Vertex<E>, Double>) queue.extractMax()).getValue();
    

    This one should definitely work according your example.