Java Reference
In-Depth Information
Client (or Client Code)
Code that interacts with a class or objects of that class.
Client programs interact with objects by sending messages to them and asking
them to perform behaviors. A major benefit of objects is that they provide reusable
pieces of code that can be used in many client programs. You've already used several
interesting objects, such as those of type String , Scanner , Random ,and File . In
other words, you and your programs have been clients of these objects. Java's class
libraries contain thousands of existing classes of objects.
As you write larger programs, however, you'll find that Java doesn't always
have a pre-existing object for the problem you're solving. For example, if you
were creating a calendar application, you might want to use objects to represent
dates, contacts, and appointments. If you were creating a three-dimensional graph-
ical simulation, you might want objects to represent three-dimensional points, vec-
tors, and matrices. If you were writing a financial program, you might want classes
to represent your various assets, transactions, and expenses. In this chapter you'll
learn how to create your own classes of objects that can be used by client programs
like these.
Our definition of object-oriented programming is somewhat simplified. A full
exploration of this programming paradigm includes other advanced concepts called
polymorphism and inheritance that will be discussed in the next chapter.
Classes and Objects
In the previous chapters, we've considered the words “class” and “program” to be
roughly synonymous. We wrote programs by creating new classes and placing static
main methods into them.
But classes have another use in Java: to serve as blueprints for new types of
objects. To create a new type of object in Java, we must create a class and add code to
it that specifies the following elements:
The state stored in each object
The behavior each object can perform
How to construct objects of that type
Once we have written the appropriate code, we can use the class to create objects
of its type. We can then use those objects in our client programs. We say that the
created objects are instances of the class because one class can be used to construct
many objects. This is similar to the way that a blueprint works: One blueprint can be
used to create many similar houses, each of which is an instance of the original
blueprint.
 
Search WWH ::




Custom Search