I swear I have the method in my class

is not abstract and does not override abstract method compareTo

Posted 22 March 2010 - 04:01 PM

First, I apologize for starting so many topics in such a short period of time. I swear this will be my last one. When I try to compile the class below, it gives me the error: Zipcode is not abstract and does not override abstract method compareTo(Zipcode), but I have defined compareTo. Does anyone know why this might be? Thank you in advance for your help. This is the last error and once I figure this out I can stop flooding the java forum with questions

Thank you. Your solution worked. However, I am still confused about generics in general. Isn't the idea that the object to be compared could be anything, not that it has to be a Zipcode, or T, whatever that may be? I'm just confused about why that worked. Thank you for the solution.

Re: is not abstract and does not override abstract method compareTo

Because Comparable is an interface, you have to implement all of its methods. Hence, you must implement its only method: compareTo()

I understand that, but I don't understand why the object type within the parentheses had to be Zipcode. Normally I just make it Object and cast later. I understand that the point of generics is to be able to avoid unnecessary casting, but I don't understand why this restricts the type of object that I can pass to my compareTo.

Re: is not abstract and does not override abstract method compareTo

Posted 22 March 2010 - 04:58 PM

The *point* of generics IS to restrict the type and to throw an error if that type is not met. This allows code to be far more robust and less bug-prone. Without generics, you could pass any object to compareTo() and the cast would would fail. With generics, the value is simply restricted to prevent such occurences.