To declare a class that implements an interface, you include animplements
clause in the class declaration. Your class can implement more than one interface, so theimplements
keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, theimplements
clause follows theextends
clause, if there is one.A Sample Interface, Relatable
Consider an interface that defines how to compare the size of objects.If you want to be able to compare the size of similar objects, no matter what they are, the class that instantiates them should implementpublic interface Relatable { // this (object calling isLargerThan) and // other must be instances of the same class // returns 1, 0, -1 if this is greater // than, equal to, or less than other public int isLargerThan(Relatable other); }Relatable
.Any class can implement
Relatable
if there is some way to compare the relative "size" of objects instantiated from the class. For strings, it could be number of characters; for books, it could be number of pages; for students, it could be weight; and so forth. For planar geometric objects, area would be a good choice (see theRectanglePlus
class that follows), while volume would work for three-dimensional geometric objects. All such classes can implement theisLargerThan()
method.If you know that a class implements
Relatable
, then you know that you can compare the size of the objects instantiated from that class.Implementing the Relatable Interface
Here is theRectangle
class that was presented in the Creating Objects section, rewritten to implementRelatable
.Becausepublic class RectanglePlus implements Relatable { public int width = 0; public int height = 0; public Point origin; // four constructors public RectanglePlus() { origin = new Point(0, 0); } public RectanglePlus(Point p) { origin = p; } public RectanglePlus(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public RectanglePlus(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } // a method for computing the area of the rectangle public int getArea() { return width * height; } // a method to implement Relatable public int isLargerThan(Relatable other) { RectanglePlus otherRect = (RectanglePlus)other; if (this.getArea() < otherRect.getArea()) return -1; else if (this.getArea() > otherRect.getArea()) return 1; else return 0; } }RectanglePlus
implementsRelatable
, the size of any twoRectanglePlus
objects can be compared.