Designing a New Class
In this lesson, we will look at how to design a new Java class.
We'll cover the following...
We'll cover the following...
Design steps
Imagine that your current programming project involves the names of people. You could represent the names as strings, but you would prefer that they were objects of a data type specifically designed for names. You decide to create your own data type. You do this by writing a class definition.
In designing a class, you need to take the following steps:
- Name the class.
- Choose and name the data fields, indicating their data types.
- Describe the behaviors associated with objects of the class.
- Name the methods that will implement these behaviors.
- Specify the methods by writing a header and descriptive comments for each one.
- Write statements that use the methods and could appear in a client.
- Revise the method specifications, if necessary.
You would implement the class—by declaring the data fields and completing the body of each method—only after you are satisfied with the class design.
Let’s design the class Name
.
Choosing the behaviors
For simplicity, we assume that a person’s name ...