Generics, Comparable and asbtract classes

Joined
Mar 23, 2010
Messages
2
Reaction score
0
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 :
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;
I get this error :
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);
	}
}
I get this error ...
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:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top