permaculture playing cards*
The moose likes Java in General and the fly likes Comparable<T> - must override compareTo()??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparable<T> - must override compareTo()???" Watch "Comparable<T> - must override compareTo()???" New topic
Author

Comparable<T> - must override compareTo()???

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Ladies/Gents,

OK, so, generics are becoming slowly clearer to me, but still having a few troubles. I implemented a function, min(), which should return the smallest element in an array. I think my function will work, but can't be sure, because now I can't compile. The specific error I'm getting is that I'm not overriding compareTo() in this class. Now, I didn't get this error until I coded the min() function, so I'm sure it's due to min()'s use of Comparable<T>. I, however, don't see why I should have to override compareTo() for the class, since I'm only using Comparable<T> with the elements of the list, which is an instance variable of this class (comes from the parent class). Would the best thing actually be to code a compareTo() for this class, even though it's kind of unnecessary for my purposes? Is my design inherently flawed, and if so, where did I go wrong? Thanks in advance!

*Update - I put in a phony compareTo() method, and the class still works fine, and the min() function works. Why did I have to put that method in? Makes me think I went about it the wrong way. Any advice/constructive criticism is always much appreciated!



Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

So here's the function.



I don't know what type "list" is, because it's declared in the base class. But in this function you assign an element of list to a variable of type T, and also to a variable of type Comparable<T>. So the elements of list have to have both those types -- only possible if T implements Comparable<T>. I'm not at all clear why you said your class didn't need to implement Comparable, or why you say the method isn't being called - you're calling it right there. Maybe I'm missing something? I think this hinges on the declaration of "list".


[Jess in Action][AskingGoodQuestions]
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Ernest Friedman-Hill wrote:
I don't know what type "list" is, because it's declared in the base class. But in this function you assign an element of list to a variable of type T, and also to a variable of type Comparable<T>. So the elements of list have to have both those types -- only possible if T implements Comparable<T>. I'm not at all clear why you said your class didn't need to implement Comparable, or why you say the method isn't being called - you're calling it right there. Maybe I'm missing something? I think this hinges on the declaration of "list".


list is an array of T that was instantiated in ArrayListClass, this class' parent class, like so:

, and then, of course, super(size) is called in this class to instantiate. I guess I did miss that I assigned both T and Comparable<T> to these elements. How should I have written this function, then, to avoid this problem? What I mean by the necessity of the compareTo() is: what good will a compareTo() function do for the class UnorderedArrayList when it is the elements of T[], which are whatever objects we instantiate them as (String, Integer, Person, Animal...) that are really being compared, and not UnorderdArrayLists? You know, I really feel like once I take a step forward with generics, I take five backward.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Try changing your class declaration as follows:

This means that T will be able to compare itself to T; the "? super T" means that T can also be comparable to super interfaces. For example, ScheduledFuture extends Delay, which again extends Comparable<Delay>. So ScheduledFuture is not a Comparable<ScheduledFuture>, but a Comparable<Delay>, and the "? super T" will make sure that ScheduledFuture will also be a match for T.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Rob Prime wrote:Try changing your class declaration as follows:

This means that T will be able to compare itself to T; the "? super T" means that T can also be comparable to super interfaces. For example, ScheduledFuture extends Delay, which again extends Comparable<Delay>. So ScheduledFuture is not a Comparable<ScheduledFuture>, but a Comparable<Delay>, and the "? super T" will make sure that ScheduledFuture will also be a match for T.



Well, I'll be darned, now I don't have to cast to Comparable<T> any more! OK, so, just to make sure I understand this: 'T extends Comparable' will make sure that any T must have its own version of compareTo(). The '? super T' is a wildcard with a lower bound, which means that it will be comparable to anything that is a superclass of T, or even another T itself. This, however, would mean that I can't compare any T against a subclass of that T. Is that correct?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

You can pass any variable to list[0].compareTo as long as it IS-A T - so that's instances of T or any subclass / implementations. That's because the actual method is compareTo(T), or compareTo(X) (with X being the direct superclass of T), or perhaps even compareTo(Object). But in the end, they all can take anything that IS-A T.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Rob Prime wrote:You can pass any variable to list[0].compareTo as long as it IS-A T - so that's instances of T or any subclass / implementations. That's because the actual method is compareTo(T), or compareTo(X) (with X being the direct superclass of T), or perhaps even compareTo(Object). But in the end, they all can take anything that IS-A T.


OK, thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparable<T> - must override compareTo()???