Java Reference
In-Depth Information
program. Procedures are in a hierarchy and data exists in data structures. In
most cases, data and applications are managed by very different subsystems.
2.3.1
Encapsulation helps to isolate change
A basic idea of object tech-
nology is that things that
belong together are packaged
together. We encapsulate
basic building blocks with
both functions and variables
that belong together, as in
figure 2.6. The building
blocks are the same, but the
organization is different. In
this way, we can package data
together with the functions
that manipulate it. This organization is much more like the way that we think,
and leads to more maintainable and elegant designs.
A class is a template where attribute types and methods are defined. It is a
complex data type. In this case, the data type not only defines the data, but
also the things that can be done to the data. We call the variables attributes
and the functions methods . For example, a tax-form object may have data for
the customer and the individual line items, and a method to calculate the
form. It might also have methods to create and destroy the form and valida-
tion methods. A class is like a cookie cutter, and an object is like the cookie.
To create an object, a program instantiates a class. For a class called Author ,
we might have objects for Tate and Hemingway .
Data
(Attributes)
Functions
(Methods)
Figure 2.6 Encapsulation packages methods and the
data structures that they use together. This packaging
scheme allows implementation hiding, which in turn
leads to formal interfaces that improve maintenance by
containing major changes to smaller areas.
2.3.2
Inheritance enables packaging of common behavior
Inheritance captures real-world relationships, allowing packaging of common
behavior without cut and paste. Inheritance is one of the primary ways that we
can customize frameworks for reuse. An inherited class keeps the interface and
features of its parent, and can override the methods to provide new behaviors,
or add new instance variables. For example, an Author class could inherit from
Person . The designers for the Author class could concentrate on building in
functionality related to being an author—say a list of published books. All of
the functionality in Person —such as the name, birth date, address, and phone
number—could be inherited. Combined with good encapsulated component
design, inheritance is another tool for reuse, and also provides better reliability.
Search WWH ::




Custom Search