Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generics, Comparable and asbtract classes

Reply
Thread Tools

Generics, Comparable and asbtract classes

 
 
Bowbaq Bowbaq is offline
Junior Member
Join Date: Mar 2010
Posts: 2
 
      03-23-2010
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 :
Quote:
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 ...
Quote:
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 by Bowbaq; 03-23-2010 at 05:26 PM..
Reply With Quote
 
 
 
 
Bowbaq Bowbaq is offline
Junior Member
Join Date: Mar 2010
Posts: 2
 
      03-25-2010
Anyone please?
Bump!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NilClass and Comparable RubyTalk@gmail.com Ruby 4 05-11-2009 01:38 PM
difference between comparator and comparable interface gaurav Java 4 04-14-2009 12:18 PM
generics and Comparable compiler warning neuneudr@yahoo.fr Java 2 01-14-2009 04:03 PM
Question regarding <=> operator and Comparable Mixin Manuel Kasten Ruby 5 11-10-2005 06:15 PM
Generics and Comparable Bergholt Java 3 11-30-2004 03:31 AM



Advertisments