Programming and Problem Solving with Java

(やまだぃちぅ) #1
6.2 Objects and Classes Revisited | 271

6.2 Objects and Classes Revisited


Let’s review what we have said about objects and see how they work in the context of pro-
gramming. Then we can more effectively explore how to solve a problem with OOD.
What is an object? We have defined an objectin three ways: as a collection of data together
with associated operations, as an entity or thing that is relevant in the context of a problem,
and as an instance of a class. What is a class? We have also defined a classin three ways: as
a description of an object that specifies the types of data values that it can hold and the op-
erations that it can perform, as a description of the behavior of a group of objects with sim-
ilar properties, and as a pattern for an object.
Although varied, these definitions are complementary rather than contradictory. In the
problem-solving phase, we look for objects (things) that are relevant to the problem at hand.
We analyze these objects and see how they interact. We abstract the common properties and
behaviors from these real objects and define classes that describe this behavior. In the im-
plementation phase, we then use these descriptions (classes) and the syntax of our pro-
gramming language to define classes (in the Java sense) that describe the data values that
an object can have and the operations that it can perform. Our application instantiates
objects of these classes that interact to solve the original problem.
Several object-oriented programming languageshave been created specifically to support OOD.
Examples include Java, C++, Visual Basic.NET, Ada 95, C#, Smalltalk, CLOS, Eiffel, and Object-
Pascal. In these languages, a classis the construct used to define the pattern employed when
instantiating an object. Let’s look first at this construct in a little more depth; in the next sec-
tion, we outline a strategy for finding objects and classes in the problem-solving phase.
As you should recognize by now, a class isn’t an object, but rather specifies a pattern to
use in creating a specific kind of object. For example, in Chapter 2 we defined a Nameclass.
Once we define the class, we can declare a variable of the class Name, such as testName,
instantiate an object with new, and assign the object’s address to testName. Here is some ex-
ample code that illustrates the process:


// Define a class
className
{
.
.
.
}
// End of Name class definition
.
.
.
// Now that we have defined the class, we can declare a variable of
//the class Name
Name testName;

Free download pdf