We communicate with objects using methods. Methods are executable code within each object, for which an interface has been established. Sometimes the interface is only for the object itself. Other times it is an interface accessible by other objects. This chapter discusses that topic in detail.
3.2 Passing Information to an Object
One convention of object-oriented programming is to provide public
methods to set and get the values of some of its private instance variables. Methods that set or modify an object’s instance variables are called
mutator methods. Methods that get or retrieve the value of an instance
variable are called accessor methods.
It is up to the designer of the class to determine which private variables require accessor and mutator methods. If you were designing a
BankAccount class, you might want a public getAccountNumber()
method, so that clients could retrieve information about their bank accounts, but you would probably not want a public getAccountPassword()
method or a public setAccountBalance() method.
In the remainder of this section, we will be concerned with mutator methods. We defined three mutator methods named takeOne(),
takeTwo(), and takeThree as part of the OneRowNim class in the previous chapter. All three of these methods change the values of the instance variables nSticks and player. All three methods have very similar
bodies. The definition of the takeOne() is:
The only difference in the bodies of the other two methods is that they
subtract 2 and 3 from nSticks instead of 1. Instead of having three, virtually identical methods, It would be a more efficient design to define a
single method where the number to be subtracted from nSticks would
be supplied as an argument when the method is called. In order to be able
to handle such an argument, we must design a new method that uses a
parameter to handle the argument.
A formal parameter, or more simply, parameter, is a variable used to
pass information into a method when the method is invoked. The type and variable name of the formal parameter must appear in the formal parameter
list that follows the method’s name in the method header. The formal
parameter is used to hold a value that is passed while the method is
executing.
Consider the following definition for a takeSticks() method:
Notice that executing the body of takeSticks() when the parameter
num stores the value 1 accomplishes precisely the same task as executing
takeOne(). If, instead, a value of 2 or 3 is stored in num, then calling the
method acts like takeTwo() or takeThree() respectively. Thus, using
parameters enables us to design methods that are more general in what
they do, which is an important principle of good program design.
Another example of a mutator method is one which defines a set
method to allow the starting number of sticks to be set for an instance of
OneRowNim. For this, we could define:
As we will see in Section 3.3, we can also define a constructor method that
can be used, when the game is created, to set the initial value of nSticks. It is often desirable to have more than one method that sets the values of
an objects’ instance variables.
If a method uses more than one parameter, use a comma to separate the
individual parameter declarations in the method header. For example, if
we wanted a method for OneRowNim that specified both the number of
sticks for the start of a game and which player takes a turn first, it could
be defined: