Java Reference
In-Depth Information
of reliance upon the GOTO. Although structured languages are a powerful tool, even they
reach their limit when a project becomes too large.
Consider this: At each milestone in the development of programming, techniques and
tools were created to allow the programmer to deal with increasingly greater complexity.
Each step of the way, the new approach took the best elements of the previous methods and
moved forward. Prior to the invention of OOP, many projects were nearing (or exceeding)
the point where the structured approach no longer works. Object-oriented methods were
created to help programmers break through these barriers.
Object-oriented programming took the best ideas of structured programming and com-
bined them with several new concepts. The result was a different way of organizing a pro-
gram. In the most general sense, a program can be organized in one of two ways: around its
code (what is happening) or around its data (what is being affected). Using only structured
programming techniques, programs are typically organized around code. This approach can
be thought of as “code acting on data.”
Object-oriented programs work the other way around. They are organized around data,
with the key principle being “data controlling access to code.” In an object-oriented lan-
guage, you define the data and the routines that are permitted to act on that data. Thus, a
data type defines precisely what sort of operations can be applied to that data.
To support the principles of object-oriented programming, all OOP languages, including
Java, have three traits in common: encapsulation, polymorphism, and inheritance. Let's ex-
amine each.
Encapsulation
Encapsulation is a programming mechanism that binds together code and the data it manip-
ulates, and that keeps both safe from outside interference and misuse. In an object-oriented
language, code and data can be bound together in such a way that a self-contained black
box is created. Within the box are all necessary data and code. When code and data are
linked together in this fashion, an object is created. In other words, an object is the device
that supports encapsulation.
Within an object, code, data, or both may be private to that object or public . Private code
or data is known to and accessible by only another part of the object. That is, private code
or data cannot be accessed by a piece of the program that exists outside the object. When
code or data is public, other parts of your program can access it even though it is defined
within an object. Typically, the public parts of an object are used to provide a controlled
interface to the private elements of the object.
Java's basic unit of encapsulation is the class . Although the class will be examined in
great detail later in this topic, the following brief discussion will be helpful now. A class
defines the form of an object. It specifies both the data and the code that will operate on that
Search WWH ::




Custom Search