public interface Comparable { public int compareTo(Object pObject); }
In order for Date and DateImpl to do this, we need to change the header of the interface Date to be:
public interface Date extends Comparableand to add the following declaration to the class declaration for DateImpl:
public int compareTo(final Object pObject) { DateImpl tDateImpl = (DateImpl)pObject; int tResult = iYear - tDateImpl.iYear; if (tResult==0) { tResult = iMonth - tDateImpl.iMonth; if (tResult==0) { tResult = iDay - tDateImpl.iDay; } } return tResult; }
The Comparable interface became part of Java when Java 2 was released in December 1998. Although most of the books that teach Java as a first language were prepared before then, a few have since been updated for Java 2. None of the ones that I've seen discuss the possibility of a class implementing the Comparable interface.