Hello.
As I found numerous answers here already, I think it's a good place to ask my question. After a couple hours googling, I found nothing so ...
As a mini project for my studdies, I have to implement a prioritized queue in Java. One of my implementations lays on a binary heap. As it needs a comparison method to keep it's heap properties, here is what I did :
I also use an abstract class Element<T> to factorize code for my various implementations :
I want to inherit this class like the folowing, and use those HeapElement<T> in my heap described before.
When I try something like this :
I get this error :
And if I try this :
I get this error ...
So I must say I'm a bit confused. The first error seems to indicate that the Comparable interface isn't implemented. And the second tells me that it is indeed.
Any hint please?
P.S I'm french, so please forgive me if I'm being unclear...
As I found numerous answers here already, I think it's a good place to ask my question. After a couple hours googling, I found nothing so ...
As a mini project for my studdies, I have to implement a prioritized queue in Java. One of my implementations lays on a binary heap. As it needs a comparison method to keep it's heap properties, here is what I did :
Code:
public class BinaryHeap<T extends Comparable<T>> {
...
}
I also use an abstract class Element<T> to factorize code for my various implementations :
Code:
public abstract class Element<T> implements Comparable<Element<T>> {
private T content;
private double priority;
public int compareTo(Element<T> e) {
return (int) (priority - e.getPriority());
}
...
}
I want to inherit this class like the folowing, and use those HeapElement<T> in my heap described before.
Code:
public class HeapElement<T> extends Element<T>{
public ElementTas(T content, double priority) {
super(content, priority);
}
}
When I try something like this :
Code:
BinaryHeap<HeapElement<T>> heap;
Bound mismatch: The type HeapElement<T> is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type BinaryHeap<T>
And if I try this :
Code:
public class HeapElement<T> extends Element<T> implements Comparable<HeapElement<T>>{
public ElementTas(T content, double priority) {
super(content, priority);
}
}
The interface Comparable cannot be implemented more than once with different arguments: Comparable<Element<T>> and Comparable<HeapElement<T>>
So I must say I'm a bit confused. The first error seems to indicate that the Comparable interface isn't implemented. And the second tells me that it is indeed.
Any hint please?
P.S I'm french, so please forgive me if I'm being unclear...
Last edited: