The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
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.
Consider an interface that defines how to compare the size of objects.
public 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);}
If you want to be able to compare the size of similar objects, no matter what they are, the class that instantiates them should implementRelatable
.
Any class can implementRelatable
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 implementsRelatable
, then you know that you can compare the size of the objects instantiated from that class.
Here is theRectangle
class that was presented in theCreating Objects section, rewritten to implementRelatable
.
public 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 required to implement // the Relatable interface 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; }}
BecauseRectanglePlus
implementsRelatable
, the size of any twoRectanglePlus
objects can be compared.
isLargerThan
method, as defined in theRelatable
interface, takes an object of typeRelatable
. The line of code, shown in bold in the previous example, castsother
to aRectanglePlus
instance. Type casting tells the compiler what the object really is. InvokinggetArea
directly on theother
instance (other.getArea()
) would fail to compile because the compiler does not understand thatother
is actually an instance ofRectanglePlus
.About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights
Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.