Java Reference
In-Depth Information
The reason why cars are successfully built is that the engineers use modularization and abstrac-
tion. They divide the car into independent modules (wheel, engine, gear box, seat, steering
wheel, etc.) and get separate people to work on separate modules independently. When a mod-
ule is built, they use abstraction. They view that module as a single component that is used to
build more-complex components.
Modularization and abstraction thus complement each other. Modularization is the process of
dividing large things (problems) into smaller parts, while abstraction is the ability to ignore de-
tails to focus on the bigger picture.
Abstraction in software
The same principles of modularization and abstraction discussed in the previous section are used
in software development. To help us maintain an overview in complex programs, we try to iden-
tify subcomponents that we can program as independent entities. Then we try to use those sub-
components as if they were simple parts, without being concerned about their inner complexities.
In object-oriented programming, these components and subcomponents are objects. If we were
trying to construct a car in software, using an object-oriented language, we would try to do what
the car engineers do. Instead of implementing the car as a single, monolithic object, we would
first construct separate objects for an engine, gearbox, wheel, seat, and so on, and then assem-
ble the car object from those smaller objects.
Identifying what kinds of objects (and with these, classes) you should have in a software system
for any given problem is not always easy, and we shall have a lot more to say about that later in
this topic. For now, we shall start with a relatively simple example. Now, back to our digital clock.
Modularization in the clock example
Let us have a closer look at the clock-display example. Using the abstraction concepts we
have just described, we want to try to find the best way to view this example so that we can
write some classes to implement it. One way to look at it is to consider it as consisting of
a single display with four digits (two digits for the hours, two for the minutes). If we now
abstract away from that very low-level view, we can see that it could also be viewed as two
separate two-digit displays (one pair for the hours and one pair for the minutes). One pair
starts at 0, increases by 1 each hour, and rolls back to 0 after reaching its limit of 23. The
other rolls back to 0 after reaching its limit of 59. The similarity in behavior of these two
displays might then lead us to abstract away even further from viewing the hours display
and minutes display distinctly. Instead, we might think of them as being objects that can
display values from zero up to a given limit. The value can be incremented, but, if the value
reaches the limit, it rolls over to zero. Now we seem to have reached an appropriate level of
abstraction that we can represent as a class: a two-digit display class.
For our clock display, we shall first program a class for a two-digit number display (Figure 3.2)
and then give it an accessor method to get its value and two mutator methods to set the value
and to increment it. Once we have defined this class, we can just create two objects of the class
with different limits to construct the whole clock display.
Search WWH ::

Custom Search