This defines a data type consisting of the set of values "pairs of floating-point numbers" (which are presumably interpreted as points in the Cartesian plane). The class includes eight methods: two constructors, two accessor methods that return the values of the data fields, two methods for converting to polar coordinates, a method for computing the distance to another Point, and a toString method. The data representation is private and can be accessed or modified only by the class methods, but the methods can be used by any client.

class Point { private double x, y; Point() { x = Math.random(); y = Math.random(); } Point(double x, double y) { this.x = x; this.y = y; } double x() { return x; } double y() { return y; } double r() { return Math.sqrt(x*x + y*y); } double theta() { return Math.atan2(y, x); } double distance(Point p) { double dx = this.x() - p.x(); double dy = this.y() - p.y(); return Math.sqrt(dx*dx + dy*dy); } public String toString() { return "(" + x + ","+y+")"; } } 

For example, no client program that uses the Point class of can refer directly to the fields p.x, q.y, and so forth (as can any client of the Point class of ) because the x and y fields are private. All that a client can do is use the public methods to process points. Those methods do have direct access to the members of any object in the class. For example, when a client invokes the distance method in with the call p.distance(q), the name x within the distance method refers to the x field in the point p (because distance was invoked as a member of the instance p), but the name p.x refers to the x field in the point q (because q is the actual parameter corresponding to the formal parameter p). We can also say this.x instead of x to eliminate possible ambiguity or confusion-the keyword this refers to the object for which a method is invoked.

Each member in a Java class has one of four possible access control states: private, protected, andpublic and no modifier (default). Informally, throughout this tutorial we use the term private to mean private or protected and the term public to mean default or public. We do so solely for economy in presenting our programs: most of our class members have no access control modifier (default); those that need to be hidden are private; and some (like main and toString) are public so as to adhere to Java system conventions.

The two added methods x() and y() in provide client programs with the capability to read the values of data fields (since they cannot access them directly). These methods do not modify the fields of the object on which they are called; they just provide their values. Such methods are known as accessor methods. We often include methods of this kind in Java classes. Note that we do not provide a way to change the values of the data fields once the object is constructed: objects that cannot be changed are said to be immutable and are common in Java. To define a different kind of Point that can move we could add a method move with the same code as the two-parameter constructor, which allows clients to change the values of the x and y fields.

illustrates the fundamental reason that we go to the trouble of carefully defining ADTs: By not allowing clients to access directly the data representation, we are free to change it! In this case, we switch to polar coordinates to represent points, but any client program should perform the same computation with one implementation as with the other.

Why would we want to make such a change? The most common reason to do so is to improve the implementation. Suppose that we have a large system with numerous client programs using an ADT and we discover a bug in the implementation of one of the methods, which causes it to give an incorrect answer in some circumstances. We have no way of knowing how the existence of that bug affects clients, but we can go ahead and fix it without having to change the clients at all. Or, in the case of the Point class, perhaps we notice that clients use the r() method frequently and the other methods infrequently. Then we can improve performance for these clients by switching to . In this case, the performance gain is relatively modest, but we shall see many examples in this tutorial where we can reap huge savings. Indeed, we will investigate many situations where the potential exists for performance gains that can vastly expand the scope of applicability of client programs, without having to change them at all.