• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparable Interface  RSS feed

 
Christopher Simmons
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have about 7 classes that all need to implement the Comparable interface. Is it better to have each implement them separately or to implement Comparable in the superclass of those 7 classes? My compareTo(Object o) method should be the same for all 7 classes with the one exception being that whatever class is executing the Comparable interface should only be comparing to a class of the same type. So I guess, what I'm asking is, is there a way to change the Object parameter to the type I need to compare to inside of the code. I would use instanceof, but no one would ever know, in the future, what I would need an instance of...

I'm trying to use Generics with this, but it doesn't make sense to do so in the superclass. Any help would be appreciated.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christopher Simmons:
...whatever class is executing the Comparable interface should only be comparing to a class of the same type...

You might be able to check for this using the Class class.

Class c1 = this.getClass();
Class c2 = other.getClass();
if(c1.equals(c2)) {...}

On the other hand, there might be a problem with this. The equals method is not overridden for Class, so it's really checking to see if both references share the same Class instance. As the API for Class states, "Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader." This suggests to me (I don't know) that if something "funny" is going on with class loaders, it might be possible to have 2 instances of Class representing the same class.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it might be possible to have 2 instances of Class representing the same class


You can make that happen by using two classloaders. Then they are not the "same" class. Makes me think this.getClass()==that.getClass() oughtta work. Or you can get their names and use string equals(). Oops, that would match them even if loaded by different classloaders. Maybe that's the right answer for you?
 
Christopher Simmons
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, guys. I half-way worked it out.

Now, here's the new deal. I have my abstract class, NameableObject, implementing Comparable<NameableObject>. In this class, I have a String variable named 'name', and an updated equals() method that checks to see whether both NameableObjects have the same class, name, and hashcode. Now, here's the problem...

My compareTo(NameableObject o) method should, according to the javadocs, be consistent with equals(), such that (x.compareTo(y)==0) == (x.equals(y)). Now, this doesn't work for me because if I have two different classes that extends NameableObject with the same text in it's 'name' variable, they are not 'supposed' to be equal.

For example... I have a class named Developer and a class named Publisher. Both extend NameableObject. My overridden equals() method in the NameableObject class works since it detects that the classes are different (It returns false). When I test my compareTo(NameableObject) method, I will get a return value of 0 when both 'name' variables are the same, but that is inconsistent with equals since they are of different types.

I guess what I'm really asking is if there is a way to have my compareTo() method consistent with equals() in this case. I would love to have my compareTo() method only compare with the parameter type for each class that uses it without generics, so i can find errors at compile time, but since the compareTo method is in the superclass with a NameableObject parameter in it already, Java will not let me implement another Comparable interface. The point is to have my NameableObject's compareTo() method good to go in terms of inheritance for all who extend it, but easier said than done!
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you get it equal, try type casting...

for example, say you have a string inputed, but you
want to make sure it's a string representation of an
int, and only an int.




when you say this.compareTo(that)==0, you're asking if the
left hand side is equal to the right hand.

so even if you get this.name = that.name you can still type
cast to see if they are alike objects.

so say "this" is of type animal and "that" is of type horse.

if their names are alike you can cast like so

animal A = (animal)that;

now unless horse is a sub class of animal that will throw an exception.

so just b/c their names are alike, you'll still get an exception because

their "types" are different.


Justin
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!