0% found this document useful (0 votes)
57 views

Implementing An Interface: A Sample Interface, Relatable

To implement an interface, a class declares that it implements the interface using an implements clause. The Relatable interface defines a method to compare the size of objects. The RectanglePlus class implements the Relatable interface by providing an implementation of the isLargerThan method that compares the areas of two rectangles. Implementing an interface allows classes to share a common behavior for comparison defined in the interface.

Uploaded by

Princess Requiso
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Implementing An Interface: A Sample Interface, Relatable

To implement an interface, a class declares that it implements the interface using an implements clause. The Relatable interface defines a method to compare the size of objects. The RectanglePlus class implements the Relatable interface by providing an implementation of the isLargerThan method that compares the areas of two rectangles. Implementing an interface allows classes to share a common behavior for comparison defined in the interface.

Uploaded by

Princess Requiso
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Implementing an Interface

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can
implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces
implemented by the class. By convention, the implements clause follows the extends clause, if there is one.

A Sample Interface, Relatable


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
implement 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 the RectanglePlus class that follows), while volume would work for
three-dimensional geometric objects. All such classes can implement the isLargerThan() 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 the Rectangle class that was presented in the Creating Objects section, rewritten to implement Relatable.

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;
}
}

String
Another commonly-used type is String, which represents texts (a sequence of characters) such as "Hello,
world". String is not a primitive type, and will be further elaborated later. In Java, a char is enclosed by
single quotes (e.g., 'A', '0'), while a String is enclosed by double quotes (e.g., "Hello"). For example,
String message = "Hello, world!"; // strings are enclosed in double-quotes
char gender = 'm';
// char is enclosed in single-quotes

You might also like