Programming and Problem Solving with Java

(やまだぃちぅ) #1
4.5 Encapsulation | 177

Here we want the elsebranch attached to the outer ifstatement, not the inner one, so we in-
dent the code as shown here. But indentation does not affect the execution of the code. Even
though the elsealigns with the first if, the compiler pairs it with the second if. An elsethat
follows a nested ifis called a dangling else. It doesn’t logically belong with the nested ifbut is
attached to it by the compiler.
To attach theelseto the firstif, not the second, you can turn the outerthenclause into a block:


if (average >= 60.0) // Correct version
{
if (average < 70.0)
System.out.println("Passing but marginal")
}
else
System.out.println("Failing");


The { }pair indicates that the first ifstatement has a compound first clause that contains
an ifstatement (with no elseclause), so the elsemust belong to the outer if.


4.5 Encapsulation


Prior to this chapter we have written Java classes without considering the larger issues of their
design and reusability. For example, we developed an initial version of the Nameclass in
Chapter 2 that was just sufficient for the purposes of the NameDriverapplication. In Chapter
3, we found that we needed to add a constructor to the Nameclass to make it usable for the
Payrollapplication. By following some common design principles and using a little fore-
thought, we can develop classes that are much more broadly useful, and that don’t require
modification for each new application.
In this section we consider the principles that result in a well-designed and
general class implementation. Primary among these is the concept ofencapsula-
tion. The dictionary provides several definitions of the wordcapsule. For example,
it can mean a sealed gelatin case that holds a dose of medication. Early spacecraft
were called capsules because they were sealed containers that carried passengers
through space. A capsule protects its contents from outside contaminants or
harsh conditions. To encapsulate something is to place it into a capsule.
What does encapsulation have to do with classes and object-oriented pro-
gramming? One goal in designing a class is to protect its contents from being
damaged by the actions of external code. If the contents of a class can be changed
only through a well-defined interface, then it is much easier to use the class and
to debug errors in an application.
Why is it important to encapsulate a class? Encapsulation is the basis for abstractionin
programming. Consider, for example, that abstraction lets us use a BufferedReaderwithout
having to know the details of its operation.
Abstraction is how we simplify the design of a large application. We design classes that
can be described in simple terms. Some implementation details of each class are irrelevant


Encapsulation Designing a
class so that its implementation
is protected from the actions of
external code except through
the formal interface
Abstraction The separation of
the logical properties of an ob-
ject from its implementation
Free download pdf