Java Reference
In-Depth Information
Polymorphism
Polymorphism allows an instruction to be given to an object using a
generalized rather than a specific, detailed command. The same command will
obtain different but somewhat predictable results depending on the object that
receives the command. For example, clicking the right mouse button in a win-
dowed environment usually displays a shortcut menu. However, the menu may
differ dramatically depending on whether you right-click a folder icon or right-
click a toolbar. While the specific actions internal to the object are different, the
command, in this case a right-click, would be generally the same.
In the case scenario, when Paul wants to open his desktop, he must perform
an open desktop operation. To open his file drawers, he must perform an open
drawer operation. Mary's rolltop desk, however, encapsulates the operation of
opening within the desk and file cabinet objects. Mary's desk and file cabinet
objects are polymorphic with respect to opening, which means Mary can give
the same command, opening, to open either object and know that this will result
in the same general operation, in which the object opens. As a user, the specific
actions internal to the object that cause it to open are not a concern to Mary
because the results are generally the same.
Many object-oriented languages, like Java, provide libraries of classes and
objects that already have been programmed to work in certain ways. Object-
oriented programmers thus can use these classes and objects in programs with-
out knowing the intricacies of the programming behind them. You simply need
to know what operations can be requested of a class or object and the results of
those operations. Table 1-3 lists 10 object-oriented programming and design
concepts in a quick reference format.
Table 1-3
Ten Object-Oriented Programming and Design Concepts
1
An object is the basic unit of organization, a combination of a data element and a set of procedures.
2
A method is the code to perform a service or operation, including tasks such as performing calculations,
storing values, and presenting results.
3
A class is an object or a set of objects that shares a common structure and a common behavior. A specific
occurrence of an object class is called an instance .
4
A subclass is a lower-level category of a class with at least one unique attribute or method of its own.
5
A subclass inherits the attributes, methods, and variables from its superclass. A superclass is a higher-level
category of class, from which the subclass inherits attributes, methods, and variables.
6
The treelike structure showing the relationship of subclasses and superclasses is called a generalization
hierarchy .
7
A message requests objects to perform a method. A message is composed of the object name and the
method.
8
An event occurs when a trigger causes an object to send a message.
9
Encapsulation is the process of hiding the implementation details of an object from its user by combining
attributes and methods.
10
Polymorphism allows instructions to be given to objects in a generalized rather than a specific, detailed
command.
 
Search WWH ::




Custom Search