-
While a program is running, we create objects from class definitions to accomplish tasks. A task can range from drawing in a paint program, to adding numbers, to depositing money in a bank account. To instruct an object to perform a task, we send a message to it.
-
In Java, we refer to these messages as methods.
-
An object can only receive a message that it understands, which means that the message must be defined within its class.
- Suppose we take the DrawingTool class (provided by this curriculum in the package gpdraw.jar) and create an object
myPencil
. In OOP terminology, we say the object myPencil
is an instance of the DrawingTool
class. An object can only be an instance of one class. We can visually represent an object with an object diagram, as shown in Figure 1.1.
-
These are some of the behaviors that the DrawingTool class provides:
forward
turnLeft
getColor
To draw a line of a specified length, we call the method forward
along with passing the distance to move the pencil. A value we pass to an object’s method is called an argument. A diagram of calling a method is shown below in Figure 1.2.