Java Reference
In-Depth Information
of object-oriented support and mention some of the principles of object-oriented
programming.
At the heart of object-oriented programming is the object . An object is a
data type that has structure and state. Each object defines operations that
may access or manipulate that state. As we have already seen, in Java an
object is distinguished from a primitive type, but this is a particular feature
of Java rather than the object-oriented paradigm. In addition to performing
general operations, we can do the following:
Objects are enti-
ties that have struc-
ture and state.
Each object defines
operations that may
access or manipu-
late that state.
Create new objects, possibly with initialization
n
Copy or test for equality
n
Perform I/O on these objects
n
Also, we view the object as an atomic unit that the user ought not to dis-
sect. Most of us would not even think of fiddling around with the bits that
represent a floating-point number, and we would find it completely ridicu-
lous to try to increment some floating-point object by altering its internal
representation ourselves.
The atomicity principle is known as information hiding . The user does
not get direct access to the parts of the object or their implementations; they
can be accessed only indirectly by methods supplied with the object. We can
view each object as coming with the warning, “Do not open no user-
serviceable parts inside.” In real life, most people who try to fix things that
have such a warning wind up doing more harm than good. In this respect,
programming mimics the real world. The grouping of data and the opera-
tions that apply to them to form an aggregate, while hiding implementation
details of the aggregate, is known as encapsulation .
An object is an
atomic unit : Its
parts cannot be
dissected by the
general users of
the object.
Information hiding
makes implementa-
tion details, includ-
ing components of
an object, inacces-
sible.
An important goal of object-oriented programming is to support code
reuse. Just as engineers use components over and over in their designs, pro-
grammers should be able to reuse objects rather than repeatedly reimple-
menting them. When we have an implementation of the exact object that we
need to use, reuse is a simple matter. The challenge is to use an existing
object when the object that is needed is not an exact match but is merely
very similar.
Object-oriented languages provide several mechanisms to support this
goal. One is the use of generic code. If the implementation is identical
except for the basic type of the object, there is no need to completely
rewrite code: Instead, we write the code generically so that it works for
any type. For instance, the logic used to sort an array of objects is inde-
pendent of the types of objects being sorted, so a generic algorithm could
be used.
Encapsulation is
the grouping of
data and the oper-
ations that apply to
them to form an
aggregate, while
hiding the imple-
mentation of the
aggregate.
 
Search WWH ::




Custom Search