Java Reference
In-Depth Information
8.1 Object-Oriented Programming
Most of our focus so far has been on procedural decomposition, the technique of
breaking complex tasks into smaller subtasks. This is the oldest style of programming,
and even in a language like Java we still use procedural techniques. But Java also pro-
vides a different approach to programming that we call object-oriented programming.
Object-Oriented Programming (OOP)
Reasoning about a program as a set of objects rather than as a set of actions.
Object-oriented programming involves a particular view of programming that has
its own terminology. Let's explore that terminology with nonprogramming examples
first. Recall the definition of object from Chapter 3.
Object
A programming entity that contains state (data) and behavior (methods).
To truly understand this definition, you have to understand the terms “state” and
“behavior.” These are some of the most fundamental concepts in object-oriented
programming.
Let's consider the class of objects we call radios. A radio can be in different states.
It can be turned on or turned off. It can be tuned to one of many different stations,
and it can be set to one of many different volumes. Any given radio has to “know”
what state it is in, which means that it has to keep track of this information internally.
We call the collection of such internal values the state of an object.
State
A set of values (internal data) stored in an object.
What are the behaviors of a radio? The most obvious one is that it produces sound
when it is turned on and the volume is turned up. But there are actions that you can
perform on a radio that manipulate its internal state. We can turn a radio on or off,
and we can change the station or volume. We can also check what station the radio is
set to right now. We call the collection of these operations the behavior of an object.
Behavior
A set of actions an object can perform, often reporting or modifying its
internal state.
Objects themselves are not complete programs; they are components that are given
distinct roles and responsibilities. Objects can be used as part of larger programs to
solve problems. The pieces of code that create and use objects are known as clients.
 
Search WWH ::




Custom Search