«^»
4.11. The role of a class

You should view a class declaration as being split into two sections. The text of the headings of the public methods (together with the names of any public fields) gives information as to what services are offered by the class. The text of the bodies of the public members together with the text of the private members give the details of how the services are to be provided.

The class declaration for Date hides three ints and provides four constructors to create a date and a rather formal way of accessing the fields of a date by means of the three access methods getYear, getMonth and getDay. This idea of hiding fields behind access methods is often termed information hiding or encapsulation.

It may seem like a complicated way of providing an object and accessing it. However, you should view the class declaration as documenting a design decision. At the moment we have chosen to represent a date by three ints. At a later stage, we may feel that that is wrong: for example, we might choose three shorts, three bytes, or a single int giving the number of days since a particular date. Since we have retained control over the access to the fields of the class by providing access methods, we can make changes like this with only minimal impact to the code of the program: we know that the only code that needs to be changed is located in the methods of the class.