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

Default Constructors

As we noted in Chapter 2, Java automatically provides a default constructor when a class does not contain a constructor.

Annotation 2020-03-23 215141

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:

Annotation 2020-03-23 215239

This explains why the following statement was valid when a class definition of OneRowNim contained no explicit definition of a constructor:

Annotation 2020-03-23 215339

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:

Annotation 2020-03-23 215533

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.

Annotation 2020-03-23 215707

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:

Annotation 2020-03-23 215905

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:

Annotation 2020-03-23 220002

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 signature:

Annotation 2020-03-23 220118