7.6. Public Version in Separate File

Classes used in a single place are often combined in the same file as in the previous examples. Often, however, classes are designed to be reused and are placed in separate files where multiple other classes can have public access to them. For instance, Listing 7.5 defines the Ship class, and Listing 7.6 defines a driver routine that tests the class. When a developer is building reusable classes, there is a much greater burden to be sure the code is documented and extensible. Two particular strategies help in this regard.

Replace public instance variables with accessor methods.

Instead of direct access to the variables, a common practice is to create a pair of helping methods to set and retrieve the values. ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.