Java Reference
In-Depth Information
multiple inheritance, you might expect to inherit this capability from another
parent class. Because this is not possible, another means must be used to ensure
that your Applet program also will implement the expected behavior appropriate
to a program listening for a button click.
As you learned in Chapter 3, a class that implements a listener interface
monitors, or listens, for events during execution of an interactive program. For
example, by implementing the listener interface, ActionListener, a class provides
the capability to listen for mouse clicks on a button, once the class is registered
as a listener for the button.
Sometimes an application may need to implement behaviors designated by
multiple interfaces. Java allows multiple implementation of interfaces rather than
multiple inheritance of classes. This is understandable when you realize that an
interface contains only method declarations and/or constants and does not pro-
vide implementations for the methods declared within it. Implementing an inter-
face provides a means of ensuring that a class will support a certain behavior
without the potential of inheriting two or more means of implementing that
behavior. Java classes inherit interface files by use of the keyword, implements;
because they must provide the code to implement all methods from the interface,
they are said to implement the interface. Because interfaces do not implement the
methods that they declare, they cannot be used by themselves to create an object.
For this reason, an interface may use the keyword, extends, to inherit from multi-
ple interfaces, although a class may not inherit from multiple classes. In this chap-
ter, two classes, LogonFrame and StockFrame, will implement multiple interfaces.
They both will implement the ActionListener interface to listen for mouse clicks
on buttons. The StockFrame class also will implement the WindowListener inter-
face in order to take certain actions when its window is opened or closed. The
LogonFrame class also will implement a new interface that you will create for
activating the display of the windows.
In addition to defining a class to create objects using inheritance, an object also
may be composed of other objects. As you learned in Chapter 1, this is called aggre-
gation. Aggregation and inheritance both provide models for object relationships.
When designing an object, decide the appropriate model to use by asking if there is
an is-a relationship (inheritance) or a has-a relationship (aggregation). If a Dog
is-a Animal, then Dog inherits from Animal. If a Dog has-a Owner, then the Dog
class may need an Owner object as a component (attribute). As each user of this
application has a password associated with the user, the User class in this chapter
uses aggregation to incorporate a Password for each User object.
Inheritance versus Aggregation
Inheritance and aggregation are not mutually exclusive. A class
both can inherit from a parent and contain an object reference.
For example, a woman both can be a daughter (is-a) and have a
daughter (has-a), and an employee both can be a supervisor (is-a)
and have a supervisor (has-a). It all depends on how your classes
are designed.
Search WWH ::




Custom Search