Quick Quiz
public class GreetingCard { private String occasion; private String message; private int length; private int width; ... // Please write a method that will determine if two GreetingCards // are equal. It should follow the conventions for such a method public boolean equals (Object o) { GreetingCard g = (GreetingCard) o; if (!occasion.equals(g.occasion)) return false; if (!message.equals(g.message)) return false; if (length != b.length) return false; if (width != b.width) return false; return true; } }Grader's notes:
- this.occasion, this.message, this.length, and this.width would also have been fine. The "this" qualifier isn't necessary, since the instance variables aren't hidden by arguments. But, it isn't incorrect to use "this", regardless.
- The equals method needed to have the same signature as above, or it wouldn't actually override the definition inherited from the Object class.
Interfaces and Implementing Them
Remember the interface specification from the Calculator class?
Let's do a quick review...
In Java, an interface specification, like a class specification, is a document. Unlike a class specification, it doesn't describe the behaviors of a class of Object. Instead, it provides a list of required behaviors. By "implementing" a standard, the designer of a class agrees to create matching methods -- and asks the compiler to ensure that the class specification actually satisfies the interface's requirements.
Interfaces are like industry standards. The communications industry has one standard so that all TV components can interact with or be connected to each other (it would be really annoying if a new DVD player could only connect with one brand of TV). By implementing an interface, a class desinger enables instances to be manipulated using not only "class reference variables", but also "instance reference variables". As a result, it can be manipulated through methods, by other objects, without the real type being known in advance -- and even if the real type was defined after-the-fact.
The Comparable Interface
Java defines an interface, Comparable, as follows:
interface Comparable { public int compareTo(Object o); }
This interface provides a uniform way to Compare implementing Objects, known as Comparables. An Object implements Comparable by doing the following:
- Adding the "implements Comparable" clause, as in "class BusinessCard implements Comparable"
- Defining a matching method.
- By convention (not enforced by Java), implementing the method so that its logical results are as follows: x.compareTo(y) has a meaning similar to (x - y). If x.equals(y), it returns 0. If (x > y), it returns a positive number. If (x < y), it returns a negative number.
An Example Comparable GreetingCard
Let's take the partial example from today's quiz and make it Comparable. In particular, let's compare GreetingCards based on their area (length * width):
public class GreetingCard implements Comparable { private String occasion; private String message; private int length; private int width; ... public int compareTo (Object o) { GreetingCard g = (GreetingCard) o; return ( (length*width) - (g.length*g.width) ); } // Please write a method that will determine if two GreetingCards // are equal. It should follow the conventions for such a method public boolean equals (Object o) { GreetingCard g = (GreetingCard) o; if (!occasion.equals(g.occasion)) return false; if (!message.equals(g.message)) return false; if (length != b.length) return false; if (width != b.width) return false; return true; } }
Next Class
...we'll take a look at another Comparable object or two and also learn to make use of Comparables.