6 Replies - 9353 Views - Last Post: 22 March 2010 - 04:58 PM Rate Topic: -----

#1 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

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 :)

/**
 The Zipcode class builds a Zipcode object containing the geographic location data corresponding with the zipcode.
 */
public class Zipcode implements Comparable<Zipcode> {
	
	/** variables to store data in the Zipcode object */
	protected int Zipcode;
	protected String Latitude;
	protected String Longitude;
	protected String City;
	protected String State;
	protected String County;
	protected String ZipType;
	
	/**
	 Constructor Zipcode builds a new Zipcode object with the 7 parameter values passed to it.
	 
	 @param	iZipcode	String representation of postal zipcode.
	 @param	iLatitude	String representation of latitude.
	 @param	iLongitude	String representation of longitude.
	 @param	iCity	String representation of city.
	 @param	iState	String representation of state.
	 @param	iCounty	String representation of county.
	 @param	iZipType	String representation of zip code type.
	 */
	public Zipcode (String iZipcode, String iLatitude, String iLongitude, String iCity, String iState, String iCounty, String iZipType) {
		Zipcode = Integer.parseInt(iZipcode);
		Latitude = iLatitude;
		Longitude = iLongitude;
		City = iCity;
		State = iState;
		County = iCounty;
		ZipType = iZipType;
	}
	

	public int getzip()
	{
		return Zipcode;
	}
		/**
	 Returns all of the Zipcode data: zipcode, latitude/longitude, city, county, state, and zipcode type.
	 @return Zipcode data.
	 */	
	
	public String toString() 
	{
		return (Zipcode+" "+Latitude+" "+Longitude+" "+City+" "+County+" "+State+" "+ZipType);
	}
	
	public int compareTo(Object zip)
	{
		if(((Zipcode)this).getzip()>=((Zipcode)zip).getzip())
			return 1;
		if(((Zipcode)this).getzip()==((Zipcode)zip).getzip())
			return 0;
		else
			return -1;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: is not abstract and does not override abstract method compareTo

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

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

Posted 22 March 2010 - 04:06 PM

you use Generics:
Comparable<Zipcode>

so you have to override the compareTo method as follows:
public int compareTo(Zipcode zip) 
        { 
                if(this.getzip()>= zip.getzip()) 
                        return 1; 
                if(this.getzip()==zip.getzip()) 
                        return 0; 
                else 
                        return -1; 
        }

Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

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

Posted 22 March 2010 - 04:08 PM

Since you're using generics, you shouldn't use Object as a parameter, you should use the generics object. Since you made the generic be Zipcode, so your code must also be:
 public int compareTo(Zipcode zip)
        {
                if (this.getzip()>= zip.getzip())
                        return 1;
                if (this.getzip()== zip.getzip())
                        return 0;
                else
                        return -1;
        }


Was This Post Helpful? 0
  • +
  • -

#4 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

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

Posted 22 March 2010 - 04:19 PM

View Postjapanir, on 22 March 2010 - 05:06 PM, said:

you use Generics:
Comparable<Zipcode>

so you have to override the compareTo method as follows:
public int compareTo(Zipcode zip) 
        { 
                if(this.getzip()>= zip.getzip()) 
                        return 1; 
                if(this.getzip()==zip.getzip()) 
                        return 0; 
                else 
                        return -1; 
        }


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.
Was This Post Helpful? 0
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 557
  • Joined: 19-February 10

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

Posted 22 March 2010 - 04:24 PM

Because Comparable is an interface, you have to implement all of its methods. Hence, you must implement its only method: compareTo()
Was This Post Helpful? 0
  • +
  • -

#6 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

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

Posted 22 March 2010 - 04:32 PM

View Postzim1985, on 22 March 2010 - 05:24 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

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.

Maybe you need to look at generics again...
http://java.sun.com/...rics/intro.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1