Saturday, June 14, 2008

Usage of Comparable interface in core Java:

Comparable interface is used to make your object comparable to other objects. Comparable interface has int compareTo(Object object). One can override this method to provide appropriate comparison of this object to the other object which is sent as an argument to this method.

Example:
Class Empoyee impements Comparable{

String name;

public int compareTo(Object otherObject){
int retValue = -1;
if(otherObject instanceOf Employee){
retValue = getName().compareTo(otherObject .getName());
}

return retValue;
}

}

No comments: