| Objects, Classes, and Interfaces

## Using Objects

> Once you've created an object, you will very likely want to use it for something. Suppose, for example, that after creating a new Rectangle, you would like to move it to a different location (say, the rectangle is an object in a drawing program and the user just clicked the mouse over the rectangle and dragged it to a new location).

> The Rectangle class provides two equivalent ways to move the rectangle:

> 1. manipulate the object's `x`, `y` variables directly
> 2. call the `move()` method
> Option 2 is often considered "more object-oriented" and safer because you manipulate the object's variables indirectly through its protective layer of methods rather than twiddling directly with them. Manipulating an object's variables directly is often considered error-prone; you could potentially put the object into an inconsistent state. However, a class would not (and should not) make its variables available for direct manipulation by other objects if it were possible for those manipulations to put the object in an inconsistent state. Java provides a mechanism whereby classes can restrict or allow access to its variables and methods by objects of another type. This section discusses calling methods and manipulating variables that have been made accessible to a other classes. To learn more about controlling access to members refer to Controlling Access to Members of a Class.

> Rectangle's `x` and `y` are accessible to other classes. So, we can assume that manipulating a Rectangle's `x` and `y` variables directly is safe.

> #### Referencing an Object's Variables

> > First, let's focus on how to inspect and modify the Rectangle's position by modifying its `x` and `y` variables directly. The next section will show you how to move the rectangle by calling its `move()` method.

> > To access an object's variables, simply append the variable name to an object reference with an intervening '.' (period).

> > >     objectReference.variable

> > Suppose you have a rectangle named `rect` in your program. You can access its `x` and `y` variables with `rect.x` and `rect.y`, respectively. Now that you have a name for `rect`'s variables, you can use those names in Java statements and expressions as though they were the names of "regular" variables. Thus to move the rectangle a new location you would write:

> > >     rect.x = 15;        // change x position
> > >     rect.y = 37;        // change y position

> > The Rectangle class has two other variables--`width` and `height`--that are accessible to objects outside the Rectangle. You can use the same notation to access them: `rect.width` and `rect.height`. So you could calculate the rectangle's area using this statement:

> > >     area = rect.height * rect.width;

> > When you access a variable through an object, you are referencing that particular object's variables. If `bob` is also a rectangle with a different height and width than `rect`, this instruction:

> > >     area = bob.height * bob.width;

> > calculates the area of the rectangle named `bob` and will give a different result than the previous instruction (which calculates the area of the rectangle named `rect`).

> > Note that the first part of the name of an object's variables (the `objectReference` in `objectReference.variable`) must be a reference to an object. While you can use a variable name here, you can also use any expression that returns an object reference. Recall that the `new` operator returns a reference to an object. So you could use the value returned from `new` to access a brand new object's variables:

> > >     height = new Rectangle().height;

> #### Calling an Object's Methods

> > Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '.' (period), and provide any arguments to the method within enclosing parentheses. If the method does not require any arguments, just use empty parentheses.

> > >     objectReference.methodName(argumentList);
> > >        or
> > >     objectReference.methodName();

> > Let's see what this means in terms of moving the rectangle. To move `rect` to a new location using its `move()` method write this:

> > >     rect.move(15, 37);

> > This Java statement calls `rect`'s `move()` method with two integer parameters, 15 and 37. This statement has the effect of moving the `rect` object by modifying its `x` and `y` variables and is equivalent to the assignment statments used previously:

> > >     rect.x = 15;
> > >     rect.y = 37;

> > If you want to move a different rectangle, the one named `bob`, to a new location you would write:

> > >     bob.move(244, 47);

> > As you see from these examples, method calls are directed at a specific object; the object specified in the method call is the object that responds to the instruction. Method calls are also known as messages. Like real-world messages, object messages must be addressed to a particular recipient. You get different results depending on which object is the recipient of the message. In the example above, when you send the object named `rect` a `move()` message, `rect` moves to the new location. When you send the object named `bob` a `move()` message, `bob` moves. Very different results. To understand messages more fully, please see the page What Are Messages?

> > A method call is an expression (see Expressions for more information) and evaluates to some value. The value of a method call is its return value, if it has one. You will often wish to assign the return value of a method to a variable or use the method call within the scope of another expression or statement. The `move()` method doesn't return a value (it's declared `void`). However, Rectangle's `inside()` method does. The `inside()` method takes an x, y coordinate and returns `true` if that point lies within the rectangle. So you could use the `inside()` method to do something special if some point, say the current mouse location, were inside the rectangle:

> > >     if (rect.inside(mouse.x, mouse.y)) {
> > >         . . .
> > >             // mouse is in the rectangle
> > >         . . .
> > >     } else {
> > >         . . .
> > >             // mouse is outside of the rectangle
> > >         . . .
> > >     }

> > Remember that the method call is a message to the named object. In this case, the object named is the Rectangle named `rect`. So:

> > >     rect.inside(mouse.x, mouse.y)

> > is asking `rect` if the mouse cursor location represented by `mouse.x` and `mouse.y` is in it. You would likely get a different response if you sent the same message to `bob`.

> > As stated previously, the `objectReference` in the method call `objectReference.method()` must be an object reference. While you can use a variable name here, you can also use any expression that returns an object reference. Recall that the `new` operator returns a reference to an object. So you could use the value returned from `new` to call a brand new object's methods:

> > >     new Rectangle(0, 0, 100, 50).equals(anotherRect)

> > The expression `new Rectangle(0, 0, 100, 50)` evaluates to an object reference that refers to a Rectangle object. So, as you can see, you can use the dot notation to call the new rectangle's `equals()` method to determine if the new rectangle is equal to the one specified in `equals()`'s argument list.

| Objects, Classes, and Interfaces