These characteristics represent a pure approach to object-
oriented programming: Feedback
1.
Everything is an object. Think of an object as a fancy
variable; it stores data, but you can “make requests” to that object,
asking it to perform operations on itself. In theory, you can take
any conceptual component in the problem you’re trying to solve
(dogs, buildings, services, etc.) and represent it as an object in your
program. Feedback
1 Some language designers have decided that object-oriented programming by itself is not
adequate to easily solve all programming problems, and advocate the combination of
various approaches into multiparadigm programming languages. See Multiparadigm Programming in Leda by Timothy Budd (Addison-Wesley 1995).
Chapter 1: Introduction to Objects
37
2.
A program is a bunch of objects telling each other
what to do by sending messages. To make a request of an
object, you “send a message” to that object. More concretely, you
can think of a message as a request to call a method that belongs to
a particular object. Feedback
3.
Each object has its own memory made up of other
objects. Put another way, you create a new kind of object by
making a package containing existing objects. Thus, you can build
complexity into a program while hiding it behind the simplicity of
objects. Feedback
4.
Every object has a type. Using the parlance, each object is an
instance of a class, in which “class” is synonymous with “type.” The
most important distinguishing characteristic of a class is “What
messages can you send to it?” Feedback
5.
All objects of a particular type can receive the same
messages. This is actually a loaded statement, as you will see
later. Because an object of type “circle” is also an object of type
“shape,” a circle is guaranteed to accept shape messages. This
means you can write code that talks to shapes and automatically
handle anything that fits the description of a shape. This
substitutability is one of the powerful concepts in OOP. Feedback
Booch offers an even more succinct description of an object:
An object has state, behavior and identity.
This means that an object can have internal data (which gives it state),
methods (to produce behavior), and each object can be uniquely
distinguished from every other object—to put this in a concrete sense,
each object has a unique address in memory2. Feedback
2 This is actually a bit restrictive, since objects can conceivably exist in different machines and address spaces, and they can also be stored on disk. In these cases, the identity of the object must be determined by something other than memory address.
38
Thinking in Java
www.BruceEckel.com
An object has an interface
Aristotle was probably the first to begin a careful study of the concept of
type; he spoke of “the class of fishes and the class of birds.” The idea that
all objects, while being unique, are also part of a class of objects that have
characteristics and behaviors in common was used directly in the first
object-oriented language, Simula-67, with its fundamental keyword class
that introduces a new type into a program. Feedback
Simula, as its name implies, was created for developing simulations such
as the classic “bank teller problem.” In this, you have a bunch of tellers,
customers, accounts, transactions, and units of money—a lot of “objects.”
Objects which are identical except for their state during a program’s
execution are grouped together into “classes of objects” and that’s where
the keyword class came from. Creating abstract data types (classes) is a
fundamental concept in object-oriented programming. Abstract data
types work almost exactly like built-in types: You can create variables of a
type (called objects or instances in object-oriented parlance) and
manipulate those variables (called sending messages or requests; you
send a message and the object figures out what to do with it). The
members (elements) of each class share some commonality: every account
has a balance, every teller can accept a deposit, etc. At the same time, each
member has its own state: each account has a different balance, each
teller has a name. Thus, the tellers, customers, accounts, transactions,
etc., can each be represented with a unique entity in the computer
program. This entity is the object, and each object belongs to a particular
class that defines its characteristics and behaviors. Feedback
So, although what we really do in object-oriented programming is create
new data types, virtually all object-oriented programming languages use
the “class” keyword. When you see the word “type” think “class” and vice
versa3. Feedback
3 Some people make a distinction, stating that type determines the interface while class is a particular implementation of that interface.
Chapter 1: Introduction to Objects
39