The Comparable interface in Java:
Java has a specific construct called an interface, which is very useful in large systems in defining the behaviour required from a class. As a first attempt, you can think of an interface as being like a class with method headers, but nothing else - no method code, no instance variables, and so on. It specifies what a class should do without defining how the class should do it. Here is an example from the Java libraries:
public interface Comparable<T>
{
int compareTo(T o);
}
The Comparable interface is useful when we want to compare objects or put them into some sort of order. A method like compareTo without any method body is known as an abstract method. All methods in an interface are automatically public (and abstract), so there is no need to write the word public in the method header within the interface. An interface can have more than one method. It can also define constants, but not variables. Any constants in an interface are implicitly defined as public static final, so they need not be declared as such. For example:
public interface RGBColours
{
int RED = 1; int GREEN = 2; int BLUE = 3;
void showColour(int colour);
}
Something a bit like a class but with only abstract methods or constants is clearly not of much use by itself. To make use of an interface we need a class to implement the interface. The class does this by providing the code for any abstract methods in the interface.
So any class that implements the Comparable interface must have a method called compareTo for the specified type T. The interface declaration specifies how to invoke the compareTo method and that it returns an int, but this is not enough. We also need something that specifies the meaning of the compareTo method - that is, what it is to do. The method should compare two objects and return an indication of which is, in some appropriate sense, the larger. Consider the following method call:
int result = x.compareTo(y);
This should set the value of result to zero if x and y are equal, some positive value if x is larger than y or a negative value if x is smaller than y.
A class can indicate that it conforms to a particular interface by using the keyword implements in the class header. For example:
class Employee implements Comparable <Employee>
{
private String name; private String address; private float salary;
...
}
The Employee class must provide a complete compareTo method, as well as its other methods. This allows the class to define an appropriate measure of comparison. For example, suppose we want to use this to rank employees by salary. We could then write the compareTo method as follows (note that the public modifier is required here, as it will not be implicit):
public int compareTo (Employee otherEmployee)
{
if (salary > otherEmployee.salary) return 1; if (salary < otherEmployee.salary) return -1; return 0;
}
How can we use this newly 'comparable' Employee classfi Java has a special class in java.util called Arrays. This offers a number of very useful operations that can be applied to arrays of objects. For example, there is a static method called sort, which can be used to arrange the objects of an array in order ... but only if they implement the Comparable interface (so they have a compareTo method). So, for example, to sort an array of Employee objects into salary order, we could write:
Employee []staff = new Employee [STAFF_COUNT];
... Arrays.sort(staff);
This enables the sort method to be general purpose - it knows how to sort any type of object, as long as each object has a compareTo method. Incidentally, it is not good enough for an object to implement its own compareTo method without declaring that it conforms to the Comparable interface. The Java compiler must be able to check at compile time that the objects being sorted have a compareTo method.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The Comparable interface in Java homework and assignments? Live The Comparable interface in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The Comparable interface in Java homework help, java assignment help and The Comparable interface in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours