System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə8/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   4   5   6   7   8   9   10   11   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

Encapsulation is a programming mechanism that binds together code and the data it
manipulates, 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 book, 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 data. Java uses a class specification to construct objects. Objects are instances of a
class. Thus, a class is essentially a set of plans that specify how to build an object.
The code and data that constitute a class are called members of the class. Specifically,
the data defined by the class are referred to as member variables or instance variables.
The code that operates on that data is referred to as member methods or just methods.
Method is Java’s term for a subroutine. If you are familiar with C/C++, it may help to
know that what a Java programmer calls a method, a C/C++ programmer calls a
function.
Polymorphism
Polymorphism (from Greek, meaning “many forms”) is the quality that allows one


interface to access a general class of actions. The specific action is determined by the
exact nature of the situation. A simple example of polymorphism is found in the
steering wheel of an automobile. The steering wheel (i.e., the interface) is the same no
matter what type of actual steering mechanism is used. That is, the steering wheel
works the same whether your car has manual steering, power steering, or rack­and­
pinion steering. Therefore, once you know how to operate the steering wheel, you can
drive any type of car.
The same principle can also apply to programming. For example, consider a stack
(which is a first­in, last­out list). You might have a program that requires three different
types of stacks. One stack is used for integer values, one for floating­point values, and
one for characters. In this case, the algorithm that implements each stack is the same,
even though the data being stored differs. In a non­object­oriented language, you would
be required to create three different sets of stack routines, with each set using different
names. However, because of polymorphism, in Java you can create one general set of
stack routines that works for all three specific situations. This way, once you know how
to use one stack, you can use them all.
More generally, the concept of polymorphism is often expressed by the phrase “one
interface, multiple methods.” This means that it is possible to design a generic interface
to a group of related activities. Polymorphism helps reduce complexity by allowing the
same interface to be used to specify a general class of action. It is the compiler’s job to
select the specific action (i.e., method) as it applies to each situation. You, the
programmer, don’t need to do this selection manually. You need only remember and
utilize the general interface.
Inheritance

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   4   5   6   7   8   9   10   11   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə