Chapter 12 P olymorphism The term p olymorphic has Greek ro - - PDF document

chapter 12 p olymorphism the term p olymorphic has greek
SMART_READER_LITE
LIVE PREVIEW

Chapter 12 P olymorphism The term p olymorphic has Greek ro - - PDF document

Chapter 12 P olymorphism The term p olymorphic has Greek ro ots and means roughly \man y forms." ( p oly = man y , morphos = form. Morphos is related to the Greek go d Morph us, who could app ear to


slide-1
SLIDE 1 Chapter 12 P
  • lymorphism
The term p
  • lymorphic
has Greek ro
  • ts
and means roughly \man y forms." (p
  • ly
= man y , morphos = form. Morphos is related to the Greek go d Morph us, who could app ear to sleeping individuals in an y form he wished and hence w as truly p
  • lymorphic.)
In biology , a p
  • lymorphic
sp ecies is
  • ne,
suc h as Homo Sapiens, that is c haracterized b y the
  • ccurrence
  • f
dieren t forms
  • r
color t yp es in individual
  • rganisms
  • r
among
  • rganisms.
In c hemistry , a p
  • lymorphic
comp
  • und
is
  • ne
that can crystallize in at least t w
  • distinct
forms, suc h as carb
  • n,
whic h can crystallize b
  • th
as graphite and as diamond. 12.1 V arieties
  • f
P
  • lymorphism
In
  • b
ject-orien ted languages, p
  • lymorphism
is a natural result
  • f
the is-a relationship and
  • f
the mec hanisms
  • f
message passing, inheritance, and the concept
  • f
substitutabilit y. One
  • f
the great strengths
  • f
the OOP approac h is that these devices can b e com bined in a v ariet y
  • f
w a ys, yielding a n um b er
  • f
tec hniques for co de sharing and reuse. Pur e p
  • lymorphism
  • ccurs
when a single function can b e applied to argumen ts
  • f
a v ariet y
  • f
t yp es. In pure p
  • lymorphism,
there is
  • ne
function (co de b
  • dy)
and a n um b er
  • f
in terpretations. The
  • ther
extreme
  • ccurs
when w e ha v e a n um b er
  • f
dieren t functions (co de b
  • dies)
all denoted b y the same name{a situation kno wn as
  • verlo
ading
  • r
sometimes ad ho c p
  • lymorphism.
Bet w een these t w
  • extremes
are
  • verriding
and deferr e d metho ds. 1 1 Note that there is little agreemen t regarding terminology in the programming language comm u- nit y . In [Horo witz 1984 ], [Marcott y 1987 ], [MacLennan 1987 ], and [Pinson 1988 ] for example, p
  • lymor-
phism is dened in a manner roughly equiv alen t to what w e are here calling
  • verlo
ading. In [Sethi 1989 ] and [Mey er 1988a ] and in the functional programming languages comm unit y (suc h as [Wikstr
  • m
1987 , Milner 1990 ]), the term is reserv ed for what w e are calling pur e p
  • lymorphism.
Other authors use the term for
  • ne,
t w
  • ,
  • r
all
  • f
the mec hanisms describ ed in this c hapter. Tw
  • complete,
but tec hnically daun ting, analyses are [Cardelli 1985 ] and [Danforth 1988 ]. 207
slide-2
SLIDE 2 208 CHAPTER 12. POL YMORPHISM 12.2 P
  • lymorphic
V ariables With the exception
  • f
  • v
erloading, p
  • lymorphism
in
  • b
ject-orien ted languages is made p
  • s-
sible
  • nly
b y the existence
  • f
p
  • lymorphic
variables and the idea
  • f
substitutabilit y . A p
  • lymorphic
v ariable is
  • ne
with man y faces; that is, it can hold v alues
  • f
dieren t t yp es. P
  • lymorphic
v ariables em b
  • dy
the principle
  • f
substitutabilit y . In
  • ther
w
  • rds,
while there is an exp ected t yp e for an y v ariable the actual t yp e can b e from an y v alue that is a subt yp e
  • f
the exp ected t yp e. In dynamically b
  • und
languages (suc h as Smalltalk), all v ariables are p
  • ten
tially p
  • lymorphic{
an y v ariable can hold v alues
  • f
an y t yp e. In these languages the desired t yp e is dened b y a set
  • f
exp ected b eha viors. F
  • r
example, an algorithm ma y mak e use
  • f
an arra y v alue, exp ecting the subscripting
  • p
erations to b e dened for a certain v ariable; an y t yp e that denes the appropriate b eha vior is suitable. Th us, the user could dene his
  • r
her
  • wn
t yp e
  • f
arra y (for example, a sparse arra y) and, if the arra y
  • p
erations w ere implemen ted using the same names, use this new t yp e with an existing algorithm. In statically t yp ed languages, suc h as Ja v a, the situation is sligh tly more complex. P
  • ly-
morphism
  • ccurs
in Ja v a through the dierence b et w een the declared (static) class
  • f
a v ariable and the actual (dynamic) class
  • f
the v alue the v ariable con tains. A go
  • d
example
  • f
a p
  • lymorphic
v ariable is the arra y allPiles in the Solitare game presen ted in Chapter 9. The arra y w as declared as main taining a v alue
  • f
t yp e Ca rdPile, but in fact it main tains v alues from eac h
  • f
the dieren t sub classes
  • f
the paren t class. A message presen ted to a v alue from this arra y , suc h as displa y in the example co de sho wn b elo w, executes the metho d asso ciated with the dynamic t yp e
  • f
the v ariable and not that
  • f
the static class. public class Solitaire extends Applet f ... static CardPile allPiles [ ]; ... public void paint(Graphics g) f for (int i = 0; i < 13; i++) allPiles[i].disp lay (g ); g ... g 12.3 Ov erloading W e sa y a function name is
  • verlo
ade d if there are t w
  • r
more function b
  • dies
asso ciated with it. Note that
  • v
erloading is a necessary part
  • f
  • v
erriding, whic h w e and will describ e
slide-3
SLIDE 3 12.3. O VERLO ADING 209 in the next section, but the t w
  • terms
are not iden tical and
  • v
erloading can
  • ccur
without
  • v
erriding. In
  • v
erloading, it is the function name that is p
  • lymorphic{it
has man y forms. Another w a y to think
  • f
  • v
erloading and p
  • lymorphism
is that there is a single abstract function that tak es v arious t yp es
  • f
argumen ts; the actual co de executed dep ends
  • n
the argumen ts giv en. The fact that the compiler can
  • ften
determine the correct function at compile time (in a strongly t yp ed language), and can therefore generate
  • nly
a single co de sequence are simply
  • ptimizations.
12.3.1 Ov erloading Messages in Real Life In Chapter 1 w e sa w an example in whic h
  • v
erloading
  • ccurred
without
  • v
erriding, when I w an ted to surprise m y friend with
  • w
ers for her birthda y . One p
  • ssible
solution w as to send the message sendFlo w ersT
  • to
m y lo cal
  • rist;
another w as to giv e the same message to m y wife. Both m y
  • rist
and m y wife (an instance
  • f
class Sp
  • use)
w
  • uld
ha v e understo
  • d
the message, and b
  • th
w
  • uld
ha v e acted
  • n
it to pro duce a similar result. In a certain sense, I could ha v e though t
  • f
sendFlo w ersT
  • as
b eing
  • ne
function understo
  • d
b y b
  • th
m y wife and m y
  • rist,
but eac h w
  • uld
ha v e used a dieren t algorithm to resp
  • nd
to m y request. Note, in particular, that there w as no inheritance in v
  • lv
ed in this example. The rst common sup erclass for m y wife and m y
  • rist
w as the category Human. But certainly the b eha vior sendFlo w ersT
  • w
as not asso ciated with all h umans. My den tist, for example, who is also a h uman, w
  • uld
not ha v e understo
  • d
the message at all. 12.3.2 Ov erloading and Co ercion As an example more closely tied to programming languages, supp
  • se
a programmer is dev el-
  • ping
a library
  • f
classes represen ting common data structures. A n um b er
  • f
data structures can b e used to main tain a collection
  • f
elemen ts (sets, bags, dictionaries, arra ys, and priorit y queues, for example), and these migh t all dene a metho d, add, to insert a new elemen t in to the collection. This situation{in whic h t w
  • totally
separate functions are used to pro vide seman tically similar actions for dieren t data t yp es{o ccurs frequen tly in all programming languages, not simply in
  • b
ject-orien ted languages. P erhaps the most common example is the
  • v
erloading
  • f
the addition
  • p
erator, +. The co de generated b y a compiler for an in teger addition is
  • ften
radically dieren t from the co de generated for a
  • ating-p
  • in
t addition, y et programmers tend to think
  • f
the
  • p
erations as a single en tit y , the \addition" function. In this example it is imp
  • rtan
t to p
  • in
t
  • ut
that
  • v
erloading ma y not b e the
  • nly
activit y taking place. A seman tically separate
  • p
eration, c
  • er
cion, is also usually asso ciated with arithmetic
  • p
erations. It
  • ccurs
when a v alue
  • f
  • ne
t yp e is con v erted in to
  • ne
  • f
a dieren t t yp e. If mixed-t yp e arithmetic is p ermitted, the addition
  • f
t w
  • v
alues ma y b e in terpreted in a n um b er
  • f
dieren t w a ys:
slide-4
SLIDE 4 210 CHAPTER 12. POL YMORPHISM
  • There
ma y b e four dieren t functions, corresp
  • nding
to in teger + in teger, in teger + real, real + in teger, and real + real. In this case, there is
  • v
erloading but no co ercion.
  • There
ma y b e t w
  • dieren
t functions for in teger + in teger and real + real. In in teger + real and real + in teger, the in teger v alue is co erced b y b eing c hanged in to a real v alue. In this situation there is a com bination
  • f
  • v
erloading and co ercion.
  • There
ma y b e
  • nly
  • ne
function, for real + real addition. All argumen ts are co erced in to b eing real. In this case there is co ercion
  • nly
, with no
  • v
erloading. 12.3.3 Ov erloading from Separate Classes There are t w
  • dieren
t forms
  • f
  • v
erloading that can b e distinguished. One form
  • ccurs
when the same function name is found in t w
  • r
more classes that are not link ed b y inher- itance. A second form
  • ccurs
when t w
  • r
more functions with the same name are found within
  • ne
class denition. The latter form will b e describ ed in the next section. A go
  • d
example
  • f
  • v
erloading
  • f
the rst t yp e is the metho d isEmpt y. This metho d is used to determine if an
  • b
ject is empt y , ho w ev er the exact meaning
  • f
empt y will dier dep ending up
  • n
circumstances. The message is understo
  • d
b y the classes V ecto r, Hashtable and Rectangle. The rst t w
  • are
collection classes, and the message returns true when there are no elemen ts in the collection. In the class Rectangle the message returns true if either the heigh t
  • r
width
  • f
a rectangle is zero, and th us the rectangle has no area. Rectangle r1 = new Rectangle (); if (r1.isEmpty()) ... Ov erloading Do es Not Imply Similarit y There is nothing in trinsic to
  • v
erloading that requires the functions asso ciated with an
  • v
erloaded name to ha v e an y seman tic similarit y . Consider a program that pla ys a card game, suc h as the solitaire game w e examined in Chapter 9. The metho d dra w w as used to dra w the image
  • f
a card
  • n
the screen. In another application w e migh t also ha v e included a dra w metho d for the pac k
  • f
cards, that is, to dra w a single card from the top
  • f
the dec k. This dra w metho d is not ev en remotely similar in seman tics to the dra w metho d for the single card, and y et they share the same name. Note that this
  • v
erloading
  • f
a single name with indep enden t and unrelated meanings should not necessarily b e considered bad st yle, and generally it will not con tribute to con- fusion. In fact, the selection
  • f
short, clear, and meaningful names suc h as add, dra w, and so
  • n,
con tributes to ease
  • f
understanding and correct use
  • f
  • b
ject-orien ted comp
  • nen
ts. It is far simpler to remem b er that y
  • u
can add an elemen t to a set than to recall that to do so requires in v
  • king
the addNewElement metho d,
  • r,
w
  • rse,
that it requires calling the routine Set Mo dule Addition Metho d.
slide-5
SLIDE 5 12.4. O VERRIDING 211 All
  • b
ject-orien ted languages p ermit the
  • ccurrence
  • f
metho ds with similar names in unrelated classes. In this case the resolution
  • f
  • v
erloaded names is determined b y
  • bser-
v ation
  • f
the class
  • f
the receiv er for the message. Nev ertheless, this do es not mean that functions
  • r
metho ds can b e written that tak e arbitrary argumen ts. The statically t yp ed nature
  • f
Ja v a still requires sp ecic declarations
  • f
all names. 12.3.4 P arameteric Ov erloading Another st yle
  • f
  • v
erloading, in whic h pro cedures (or functions
  • r
metho ds) in the same con text are allo w ed to share a name and are disam biguated b y the n um b er and t yp e
  • f
argumen ts supplied, is called p ar ameteric
  • verlo
ading; it
  • ccurs
in Ja v a as w ell as in some imp erativ e languages (suc h as Ada) and man y functional languages. P arameteric
  • v
erload- ing is most
  • ften
found in constructor functions. A new Rectangle, for example, can b e created either with no argumen ts (generating a rectangle with size zero and north w est cor- ner 0,0), with t w
  • in
teger argumen ts (a width and heigh t), with four in teger argumen ts (width, heigh t, north w est corner), with a P
  • int
(the north w est corner, size is zero), with a Dimension (heigh t and width, corner 0,0),
  • r
with b
  • th
a P
  • int
and a Dimension. Rectangle r1 = new Rectangle (); Rectangle r2 = new Rectangle (6, 7); Rectangle r3 = new Rectangle (10, 10, 6, 7); Point p1 = new Point (10, 10); Dimension d1 = new Dimension (6, 7); Rectangle r4 = new Rectangle (p1); Rectangle r5 = new Rectangle (d1); Rectangle r6 = new Rectangle (p1, d1); There are six dieren t constructor functions in this class, all with the same name. The compiler decides whic h function to execute based
  • n
the n um b er and t yp e
  • f
argumen ts used with the function call. Ov erloading is a necessary prerequisite to the
  • ther
forms
  • f
p
  • lymorphism
w e will consider:
  • v
erriding, deferred metho ds, and pure p
  • lymorphism.
It is also
  • ften
useful in reducing the \conceptual space," that is, in reducing the amoun t
  • f
information that the programmer m ust remem b er. Often, this reduction in programmer-memory space is just as signican t as the reduction in computer-memory space p ermitted b y co de sharing. 12.4 Ov erriding In Chapter 8 w e describ ed the mec hanics
  • f
  • v
erriding, so it is not necessary to rep eat that discussion here. Recall, ho w ev er, the follo wing essen tial elemen ts
  • f
the tec hnique. In
  • ne
class (t ypically an abstract sup erclass), there is a general metho d dened for a particular
slide-6
SLIDE 6 212 CHAPTER 12. POL YMORPHISM message that is inherited and used b y sub classes. In at least
  • ne
sub class, ho w ev er, a metho d with the same name is dened, that hides access to the general metho d for instances
  • f
this class (or, in the case
  • f
renemen t, subsumes access to the general metho d). W e sa y the second metho d
  • verrides
the rst. Ov erriding is
  • ften
transparen t to the user
  • f
a class, and, as with
  • v
erloading, frequen tly the t w
  • functions
are though t
  • f
seman tically as a single en tit y . 12.4.1 Replacemen t and Renemen t In Chapter 9 w e briey noted that
  • v
erriding can
  • ccur
in t w
  • dieren
t forms. A metho d can r eplac e the metho d in the paren t class, in whic h case the co de in the paren t is not executed at all. Alternativ ely , the co de from the c hild can b e used to form a r enement, whic h com bines the co de from the paren t and the c hild classes. Normally ,
  • v
erridden metho ds use replacemen t seman tics. If a renemen t is desired, it can b e constructed b y explicitly in v
  • king
the paren t metho d as a function. This is accom- plished b y using the pseudo-v ariable sup er as the receiv er in a message passing expression. An example from the Solitare program describ ed in Chapter 9 sho w ed this: class DiscardPile extends CardPile f public void addCard (Card aCard) f if (! aCard.faceUp()) aCard.flip(); super.addCard(a Car d) ; g g Constructors,
  • n
the
  • ther
hand, always use renemen t seman tics. A constructor for a c hild class will alw a ys in v
  • k
e the constructor for the paren t class. This in v
  • cation
will tak e place b efor e the co de for the constructor is executed. If the constructor for the paren t class requires argumen ts, the pseudo-v ariable sup er is used as if it w ere a function: class DeckPile extends CardPile f DeckPile (int x, int y) f // rst initialize paren t super(x, y); // then create the new dec k // rst put them in to a lo cal pile for (int i = 0; i < 4; i++) for (int j = 0; j <= 12; j++)
slide-7
SLIDE 7 12.5. ABSTRA CT METHODS 213 addCard(new Card(i, j)); // then sh ue the cards Random generator = new Random(); for (int i = 0; i < 52; i++) f int j = Math.abs(generat
  • r
.n ext In t() ) % 52; // sw ap the t w
  • card
v alues Object temp = thePile.elementAt (i ); thePile.setEleme ntA t( the Pi le .el em ent At (j ), i); thePile.setEleme ntA t( tem p, j); g g g When used in this fashion, the call
  • n
the paren t constructor m ust b e the rst statemen t executed. If no call
  • n
sup er is mak e explicitly and there exist t w
  • r
more
  • v
erloaded forms
  • f
the constructor, the constructor with no argumen ts (sometimes called the default constructor) will b e the form used. 12.5 Abstract Metho ds A metho d that is declared as abstr act can b e though t
  • f
as dening a metho d that is deferr e d; it is sp ecied in the paren t class but m ust b e implemen ted in the c hild class. In terfaces can also b e view ed as a metho d for dening deferred classes. Both can b e considered to b e a generalization
  • f
  • v
erriding. In b
  • th
cases, the b eha vior describ ed in a paren t class is mo died b y the c hild class. In an abstract metho d, ho w ev er, the b eha vior in the paren t class is essen tially n ull, a place holder, and al l useful activit y is dened as part
  • f
the co de pro vided b y the c hild class. One adv an tage
  • f
abstract metho ds is conceptual, in that their use allo ws the program- mer to think
  • f
an activit y as asso ciated with an abstraction at a higher lev el than ma y actually b e the case. F
  • r
example, in a collection
  • f
classes represen ting geometric shap es, w e can dene a metho d to dra w the shap e in eac h
  • f
the sub classes Circle, Squa re, and T riangle. W e could ha v e dened a similar metho d in the paren t class Shap e, but suc h a metho d cannot, in actualit y , pro duce an y useful b eha vior since the class Shap e do es not ha v e sucien t information to dra w the shap e in question. Nev ertheless, the mere presence
  • f
this metho d p ermits the user to asso ciate the concept dr aw with the single class Shap e, and not with the three separate concepts Squa re, T riangle, and Circle. There is a second, more practical reason for using abstract metho ds. In statically t yp ed
  • b
ject-orien ted languages, suc h as Ja v a, a programmer is p ermitted to send a message to an
  • b
ject
  • nly
if the compiler can determine that there is in fact a corresp
  • nding
metho d
slide-8
SLIDE 8 214 CHAPTER 12. POL YMORPHISM that matc hes the message selector. Supp
  • se
the programmer wishes to dene a p
  • lymorphic
v ariable
  • f
class Shap e that will, at v arious times, con tain instances
  • f
eac h
  • f
the dieren t shap es. Suc h an assignmen t is p
  • ssible,
according to
  • ur
rule
  • f
substitutabilit y; nev erthe- less, the compiler will p ermit the message dra w to b e used with this v ariable
  • nly
if it can ensure that the message will b e understo
  • d
b y an y v alue that ma y b e asso ciated with the v ariable. Assigning a metho d to the class Shap e eectiv ely pro vides this assurance, ev en when the metho d in class Shap e is nev er actually executed. 12.6 Pure P
  • lymorphism
Man y authors reserv e the term p
  • lymorphism
(or pur e p
  • lymorphism)
for situations where
  • ne
function can b e used with a v ariet y
  • f
argumen ts, and the term
  • v
erloading for situ- ations where there are m ultiple functions all dened with a single name. 2 Suc h facilities are not restricted to
  • b
ject-orien ted languages. In Lisp
  • r
ML, for example, it is easy to write functions that manipulate lists
  • f
arbitrary elemen ts; suc h functions are p
  • lymorphic,
b ecause the t yp e
  • f
the argumen t is not kno wn at the time the function is dened. The abil- it y to form p
  • lymorphic
functions is
  • ne
  • f
the most p
  • w
erful tec hniques in
  • b
ject-orien ted programming. It p ermits co de to b e written
  • nce,
at a high lev el
  • f
abstraction, and to b e tailored as necessary to t a v ariet y
  • f
situations. Usually , the programmer accomplishes this tailoring b y sending further messages to the receiv er for the metho d. These subsequen t messages
  • ften
are not asso ciated with the class at the lev el
  • f
the p
  • lymorphic
metho d, but rather are deferred metho ds dened in the lo w er classes. An example will help us to illustrate this concept. As w e noted in Chapter 8, the class Numb er is an abstract class, paren t to the wrapp er classes suc h as Integer, Double, Float. The denition
  • f
the class is similar to the follo wing: public abstract class Number f public abstract int intValue(); public abstract long longValue(); public abstract float floatValue(); public abstract double doubleValue(); 2 The extreme cases ma y b e easy to recognize, but disco v ering the line that separates
  • v
erloading from p
  • lymorphism
can b e dicult. In b
  • th
Ja v a and ML a programmer can dene a n um b er
  • f
functions, eac h ha ving the same name, but whic h tak e dieren t argumen ts. Is it
  • v
erloading in Ja v a b ecause the v arious functions sharing the same name are not dened in
  • ne
lo cation, whereas in ML-st yle p
  • lymorphism
they m ust all b e bundled together under a single heading?
slide-9
SLIDE 9 12.7. EFFICIENCY AND POL YMORPHISM 215 public byte byteValue() f return (byte) intValue(); g public short shortValue() f return (short) intValue(); g g The metho d intV alue is abstract and deferred{eac h t yp e
  • f
n um b er m ust pro vide their
  • wn
implemen tation
  • f
this metho d. The metho d b yteV alue,
  • n
the
  • ther
hand, is not
  • v
erridden. It is a purely p
  • lymorphic
algorithm. Regardless
  • f
whether the receiv er is an in teger, a double precision
  • ating
p
  • in
t v alue,
  • r
some
  • ther
t yp e
  • f
n um b er, this is the
  • nly
denition that will b e found. F
  • r
all
  • f
these dieren t t yp es, when b yteV alue is in v
  • k
ed this will b e the algorithm that is executed. The imp
  • rtan
t dening c haracteristic
  • f
pure p
  • lymorphism,
as
  • pp
  • sed
to
  • v
erloading and
  • v
erriding, is that there is
  • ne
function with the giv en name, used with a v ariet y
  • f
dieren t argumen ts. Almost alw a ys, as in this case, the b
  • dy
  • f
suc h an algorithm will mak e use
  • f
  • ther
forms
  • f
p
  • lymorphism,
suc h as the in v
  • cation
  • f
abstract functions sho wn here. 12.7 Eciency and P
  • lymorphism
An essen tial p
  • in
t to note is that programming alw a ys in v
  • lv
es compromises. In particular, programming with p
  • lymorphism
in v
  • lv
es compromises b et w een ease
  • f
dev elopmen t and use, readabilit y , and eciency . In large part, eciency has b een already considered and dismissed; ho w ev er, it w
  • uld
b e remiss not to admit that it is an issue, ho w ev er sligh t. A function, suc h as the b yteV alue metho d describ ed in the last section, that do es not kno w the t yp e
  • f
its argumen ts can seldom b e as ecien t as a function that has more complete information. Nev ertheless, the adv an tages
  • f
rapid dev elopmen t and consisten t application b eha vior and the p
  • ssibilities
  • f
co de reuse usually more than mak e up for an y small losses in eciency . 12.8 Chapter Summary P
  • lymorphism
is an um brella term that is used to describ e a v ariet y
  • f
dieren t mec hanisms found in programming languages. In
  • b
ject-orien ted languages the most imp
  • rtan
t forms
  • f
p
  • lymorphism
are tied to the p
  • lymorphic
v ariable{a v ariable that can hold man y dieren t t yp es
  • f
v alues. F
  • r
example,
  • v
erloading
  • ccurs
when t w
  • r
more functions share the same name. If these functions happ en to b e found in classes that ha v e a paren t class/c hild class relationship, then it is called
  • v
erriding. If an
  • v
erridden function is used with a p
  • lymorphic
v ariable, then the particular function executed will b e determined b y the run-time v alue
  • f
the v ariable, not the compile-time declaration for the v ariable.
slide-10
SLIDE 10 216 CHAPTER 12. POL YMORPHISM Other forms
  • f
p
  • lymorphism
include
  • v
erloading from indep enden t classes, parameteric
  • v
erloading (o v erloading that is disam biguated b y the t yp es
  • f
argumen ts used in a function call), and abstract metho ds. Note that the use
  • f
p
  • lymorphism
tends to
  • ptimize
program dev elopmen t time and reliabilit y , at the cost
  • f
run-time eciency . F
  • r
most programs, the b enets far exceed the costs. F urther Reading In the in terests
  • f
completeness, it should b e men tioned that there is at least
  • ne
imp
  • rtan
t st yle
  • f
p
  • lymorphism,
found in
  • ther
computer languages, that is not found in Ja v a. A generic (sometimes called a template) is a tec hnique that allo ws a class description to b e parameterized with a t yp e. In C++, for example,
  • ne
could declare a class as follo ws: template <class T> class box f public: box (T init) f value = initial; g T getValue() f return value; g private T value; g; The result is a \b
  • x
  • f
T", and not simply a b
  • x.
T
  • create
suc h a v alue,
  • ne
m ust also sp ecify a t yp e for the parameter v alue T: box<int> aBox(5); // create a b
  • x
with an in teger box<Shape> aBox(Circle); // create a b
  • x
with a circle One imp
  • rtan
t place where this mec hanism is useful is in the creation
  • f
collection classes (see Chapter 19). A language with generics, for example, w
  • uld
allo w
  • ne
to declare a v ector
  • f
Car ds, rather than (as in Ja v a) simply a v ector
  • f
  • b
jects. The compiler can then v erify that the collection con tains
  • nly
the indicated t yp e
  • f
v alues. More imp
  • rtan
tly , the compiler can a v
  • id
the cast necessary in Ja v a when an
  • b
ject is remo v ed from a con tainer A discussion
  • f
generics in relation to
  • ther
forms
  • f
p
  • lymorphism
can b e found in [Budd 97 ]. Study Questions 1. What do es the term p
  • lymorphic
mean in common usage? 2. What is a p
  • lymorphic
v ariable?
slide-11
SLIDE 11 EXER CISES 217 3. Ho w is the c haracterization
  • f
p
  • lymorphic
v ariables dieren t in dynamically t yp ed languages than in staticly t yp ed languages? 4. What do es it mean to sa y that a function name is
  • v
erloaded? 5. What do es it mean to sa y that a v alue has b een co erced to a dieren t t yp e? 6. What is parameteric
  • v
erloading? 7. What is
  • v
erriding, and ho w is it dieren t from
  • v
erloading? 8. What is the dierence b et w een
  • v
erriding using replacemen t, and
  • v
erriding using renemen t? 9. What is the default seman tics for
  • v
erriding for metho ds? F
  • r
constructors? 10. What is an abstract metho d? 11. Ho w is an abstract metho d denoted? 12. What c haracterizes pure p
  • lymorphism?
13. Wh y should a programmer not b e
  • v
erly concerned with the loss
  • f
eciency due to the use
  • f
p
  • lymorphic
programming tec hniques? Exercises 1. Describ e the v arious t yp es
  • f
p
  • lymorphism
found in the Pin ball game application presen ted in Chapter 7. 2. Describ e the v arious t yp es
  • f
p
  • lymorphism
found in the Solitare application presen ted in Chapter 9.