Chapter 8. Designing Classes

CHAPTER GOALS

  • To learn how to choose appropriate classes for a given problem

  • To understand the concepts of cohesion and coupling

  • To minimize the use of side effects

  • To document the responsibilities of methods and their callers with preconditions and postconditions

  • To understand static methods and variables

  • To understand the scope rules for local variables and instance variables

  • To learn about packages

    T To learn about unit testing frameworks

In this chapter you will learn more about designing classes. First, we will discuss the process of discovering classes and declaring methods. Next, we will discuss how the concepts of pre- and postconditions enable you to specify, implement, and invoke methods correctly. You will also learn about several more technical issues, such as static methods and variables. Finally, you will see how to use packages to organize your classes.

Discovering Classes

You have used a good number of classes in the preceding chapters and probably designed a few classes yourself as part of your programming assignments. Designing a class can be a challenge—it is not always easy to tell how to start or whether the result is of good quality.

What makes a good class? Most importantly, a class should represent a single conceptfrom a problem domain. Some of the classes that you have seen represent concepts from mathematics:

  • Point

  • Rectangle

  • Ellipse

Note

A class should represent a single concept from a problem domain, such as business, science, or mathematics. ...

Get Big Java, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.