Methods: Communicating with Objects
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.3 Constructor Methods
As we noted in Chapter 2, Java automatically provides a default constructor
when a class does not contain a constructor.
The default constructor’s role is simply to create an instance (an object) of
that class. It takes no parameters. In terms of what it does, the default
constructor for OneRowNim would be equivalent to a public constructor
method with an empty body:
This explains why the following statement was valid when a class definition of OneRowNim contained no explicit definition of a constructor:
The first is an explicit representation of the default constructor. The second is the constructor we defined earlier to initialize the number of sticks
in a OneRowNim object. Having multiple constructors lends flexibility to
the design of a class. In this case, the first constructor merely accepts
OneRowNim’s default initial state. The second enables the user to initialize
the number of sticks to something other than the default value.
In Java, as in some other programming languages, when two different
methods have the same name, it is known as method overloading. In overloading
this case, OneRowNim is used as the name for two distinct constructor
methods. What distinguishes one constructor from another is its signature, which consists of its name together with the number and types of
formal parameters it takes. Thus, our OneRowNim constructors have the
following distinct signatures:
Both have the same name, but the first takes no parameters, whereas the
second takes a single int parameter.
The same point applies to methods in general. Two methods can have Methods are known by their
the same name as long as they have distinct signatures. A method signature consists of its name, and the number, types, and order of its formal
parameters. A class may not contain two methods with the same signature, but it may contain several methods with the same name, provided
each has a distinct signature.
There is no limit to the amount of overloading that can be done in designing constructors and methods. The only restriction is that each method
have a distinct signature. For example, suppose in addition to the two
constructors we have already defined, we want a constructor that would
let us set both the number of sticks and the player who starts first. The
following constructor will do what we want:
When calling this constructor, we would have to take care to pass the number of sticks as the value of the first argument and either 1 or 2 as the value
of the second argument:
If we mistakenly reversed 14 and 2 in the first of these statements, we
would end up with a OneRowNim game that starts with 2 sticks and has
player 14 as the player with the first move.
We have now defined three constructor methods for the OneRowNim
class. Each constructor has the name OneRowNim, but each has a distinct