Overridding compareTo() method

Hi all,

I have the following MyDate class:

class MyDate{

int month;// Month number in range 1 to 12.

int day;// Day number in range 1 to 31.

int year;// Year number.

MyDate(int m,int d,int y){// Convenience constructor.

month = m;

day = d;

year = y;

}

}

I want to modify the MyDate class so that it implements the Comparable interface.

I modified it in this way:

import java.util.*;

class MyDateimplements Comparable

{

int month;// Month number in range 1 to 12.

int day;// Day number in range 1 to 31.

int year;// Year number.

// Convenience constructor.

MyDate(int m,int d,int y)

{

month = m;

day = d;

year = y;

}

publicint compareTo(MyDate myDate)

{

GregorianCalendar gc1 =new GregorianCalendar(this.year, this.month - 1, this.day);

GregorianCalendar gc2 =new GregorianCalendar(myDate.year, myDate.month - 1, myDate.day);

int result = gc1.compareTo(gc2);

if (result == 0)

{

System.out.println("Dates are equal.");

return 0;

}

elseif (result < 0)

{

System.out.println("gc1 is before gc2.");

return -1;

}

elseif (result > 0)

{

System.out.println("gc1 is after gc2.");

return 1;

}

}

}

However, I receive the following compile error:

MyDate.java:3: MyDate is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable

class MyDate implements Comparable

^

1 error

I don't know what's wrong? Any help is greatly appreciated.

[3423 byte] By [RonitTa] at [2007-11-27 2:38:18]
# 1
Note the difference between:compareTo(Object other)andcompareTo(MyDate myDate)
Herko_ter_Horsta at 2007-7-12 2:59:20 > top of Java-index,Java Essentials,Java Programming...
# 2
> Note the difference between:> compareTo(Object other)> and> compareTo(MyDate myDate)Thanks for the reply. However, I don't understand the point, MyDate is an Object, right? compareTo(Object other) can accept any kind of object as argument, right?
RonitTa at 2007-7-12 2:59:20 > top of Java-index,Java Essentials,Java Programming...
# 3

> > Note the difference between:

> > compareTo(Object other)

> > and

> > compareTo(MyDate myDate)

>

> Thanks for the reply. However, I don't understand the

> point, MyDate is an Object, right? compareTo(Object

> other) can accept any kind of object as argument,

> right?

MyDate is a type of Object, but it isn't an Object. If you have two methods with two different signatures (one that takes an Object and one that takes a MyDate) then they are considered two different methods. You aren't overriding the Object version, you're creating a new version that takes a different parameter.

Make your version take an Object, so that you'll actually override the superclass' version. The cast it to a MyDate. If the parameter isn't a MyDate, then you can throw an exception or whatever. If it is, then do your comparison.

hunter9000a at 2007-7-12 2:59:20 > top of Java-index,Java Essentials,Java Programming...
# 4
BTW, as of Java 5, Comparable is a generic type. If you declare your class to implement Comparable<MyDate>, you can keep the method signature as it is now.
Herko_ter_Horsta at 2007-7-12 2:59:20 > top of Java-index,Java Essentials,Java Programming...