Java Reference
In-Depth Information
miles driven (i.e., its odometer reading). Like its capabilities, the car's attributes are repre-
sented as part of its design in its engineering diagrams (which, for example, include an
odometer and a fuel gauge). As you drive an actual car, these attributes are carried along
with the car. Every car maintains its own attributes. For example, each car knows how
much gas is in its own gas tank, but not how much is in the tanks of other cars.
An object, similarly, has attributes that it carries along as it's used in a program. These
attributes are specified as part of the object's class. For example, a bank-account object has
a balance attribute that represents the amount of money in the account. Each bank-
account object knows the balance in the account it represents, but not the balances of the
other accounts in the bank. Attributes are specified by the class's instance variables .
1.5.7 Encapsulation and Information Hiding
Classes (and their objects) encapsulate , i.e., encase, their attributes and methods. A class's
(and its object's) attributes and methods are intimately related. Objects may communicate
with one another, but they're normally not allowed to know how other objects are imple-
mented—implementation details are hidden within the objects themselves. This informa-
tion hiding , as we'll see, is crucial to good software engineering.
1.5.8 Inheritance
A new class of objects can be created conveniently by inheritance —the new class (called
the subclass ) starts with the characteristics of an existing class (called the superclass ), pos-
sibly customizing them and adding unique characteristics of its own. In our car analogy,
an object of class “convertible” certainly is an object of the more general class “automo-
bile,” but more specifically , the roof can be raised or lowered.
1.5.9 Interfaces
Java also supports interfaces —collections of related methods that typically enable you to
tell objects what to do , but not how to do it (we'll see an exception to this in Java SE 8).
In the car analogy, a “basic-driving-capabilities” interface consisting of a steering wheel, an
accelerator pedal and a brake pedal would enable a driver to tell the car what to do. Once
you know how to use this interface for turning, accelerating and braking, you can drive
many types of cars, even though manufacturers may implement these systems differently .
A class implements zero or more interfaces, each of which can have one or more
methods, just as a car implements separate interfaces for basic driving functions, control-
ling the radio, controlling the heating and air conditioning systems, and the like. Just as
car manufacturers implement capabilities differently , classes may implement an interface's
methods differently . For example a software system may include a “backup” interface that
offers the methods save and restore . Classes may implement those methods differently,
depending on the types of things being backed up, such as programs, text, audios, videos,
etc., and the types of devices where these items will be stored.
1.5.10 Object-Oriented Analysis and Design (OOAD)
Soon you'll be writing programs in Java. How will you create the code (i.e., the program
instructions) for your programs? Perhaps, like many programmers, you'll simply turn on
 
 
 
 
 
Search WWH ::




Custom Search