Java Reference
In-Depth Information
Encapsulation
Encapsulation is the capability of an object to have data (properties) and
functionality (methods) available to the user, without the user having to under-
stand the implementation within the object — the closed box concept presented
earlier. Traditional structured programming separates data from procedures,
which are sections of a program that perform a specific task. In the object-
oriented world, an object contains methods as well as its associated data.
Encapsulation is the process of hiding the implementation details of an object
from its user, making those details transparent. In programming, an action is
transparent if it takes place without any visible effect other than the desired
output. Transparency is a good characteristic of a system because it shields the
user from the system's complexity. For example, you do not need to know how
the internal parts of a DVD player work in order to view a movie.
This process of making the implementation and programming details trans-
parent to the user sometimes is called information hiding . Providing access to
an object only through its messages, while keeping the details private, is an
example of information hiding. Users know what operations can be requested of
an object, but do not know the specifics of how the operations are performed.
Encapsulation allows objects to be modified without requiring that the applica-
tions that use them also be modified.
In the case scenarios, both Paul and Mary want drawers that cannot be
pulled all the way out accidentally. In constructing his system, Paul had to attend
to the details of how drawer stops work, which ones to use, and how to build
them into the system. For Mary, the safety-stop functionality and behavior is
encapsulated within the file cabinet object. As an object-oriented programmer,
she does need to understand how her system is constructed. This is not to say
that Paul understands his system better than Mary does. From a user's point of
view, however, the object-oriented nature of her system means that Mary does
not need to concern herself with how the safety stops on her drawers work —
only that they do work.
Inheritance
Inheritance means that a programmer can use a class, along with its
functions and data, to create a subclass, which saves time and coding. A subclass
has at least one attribute or method that differs from its superclass, but it inher-
its functions and data of the superclass. Also known as subclassing , this is a very
efficient way of reusing code, and provides a way for programmers to define a
subclass as an extension of another class without copying the definition. If you
let a subclass inherit from a superclass, it automatically will have all the data and
methods of the superclass.
In the case scenario, Mary's desk, chair, and cabinet all have similar wood
grain, color, and style. If you think of the furniture as a superclass, then Mary's
individual pieces are subclasses of that furniture line. Because they are subclasses
of the same superclass, they inherited the same wood grain, color, and style
attributes from the superclass.
Search WWH ::




Custom Search