Java Reference
In-Depth Information
memory space to store the relevant data, that is, the string and its length. Next, we must
identify the type of operations performed on a string. Some of the operations on a string
might be to replace a particular character of a string, extract part of a string, change a
string from uppercase to lowercase, and so on. The class String provides the necessary
operations to be performed on a string.
As another example of how an object contains both data and operations on that data,
consider objects of type JButton . Because every button has a label, which is a string,
every button must have memory space to store its label. Some of the operations on a
button that you have encountered are to set the label of the button and to add a listener
object to a button. Other operations that can be performed on a button are to set its size
and location. These operations are the methods of a class. Thus, the class JButton
provides the methods to set a button's size and location.
A Simplified OOD Methodology
Now that you have an overview of objects and the essential components of OOD, you
may be eager to learn how to solve a particular problem using OOD methodology. The
best way to learn is by practice. A simplified OOD methodology can be expressed as
follows:
1. Write down a detailed description of the problem.
2. Identify all the (relevant) nouns and verbs.
3. From the list of nouns, select the objects. Identify the data components
of each object.
4. From the list of verbs, select the operations.
In item 3, after identifying the objects or classes, usually you will realize that several
objects function in the same way. That is, they have the same data components and same
operations. In other words, they will lead to the construction of the same class.
Remember that objects are nothing but instances of a particular class. Therefore, to
create objects you have to learn how to create classes. In other words, to create objects
you first need to create classes; to know what type of classes to create, you need to
know what an object stores and what operations are needed to manipulate an object's
data. You can see that objects and classes are closely related. Because an object consists
of data and operations on the data in a single unit, in Java we use the mechanism of
classes to combine data and its operations in a single unit. In OOD methodology, we
therefore identify classes, data members of classes, and operations. In Java, data members
are also known as fields.
The remainder of this section gives various examples to illustrate how objects, data
components of objects, and operations on data are identified. In these examples, nouns
(objects) are in bold type, and verbs (operations) are in italics.
 
Search WWH ::




Custom Search