chapter 12 p olymorphism the term p olymorphic has greek
play

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


  1. 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 sleeping individuals in an y form he wished and hence w as truly p olymorphic.) In biology , a p olymorphic sp ecies is one, suc h as Homo Sapiens , that is c haracterized b y the o ccurrence of di�eren t forms or color t yp es in individual organisms or among organisms. In c hemistry , a p olymorphic comp ound is one that can crystallize in at least t w o distinct forms, suc h as carb on, whic h can crystallize b oth as graphite and as diamond. 12.1 V arieties of P olymorphism In ob ject-orien ted languages, p olymorphism is a natural result of the is-a relationship and of the mec hanisms of message passing, inheritance, and the concept of substitutabilit y. One of the great strengths of the OOP approac h is that these devices can b e com bined in a v ariet y of w a ys, yielding a n um b er of tec hniques for co de sharing and reuse. Pur e p olymorphism o ccurs when a single function can b e applied to argumen ts of a v ariet y of t yp es. In pure p olymorphism, there is one function (co de b o dy) and a n um b er of in terpretations. The other extreme o ccurs when w e ha v e a n um b er of di�eren t functions (co de b o dies) all denoted b y the same name{a situation kno wn as overlo ading or sometimes 1 ad ho c p olymorphism . Bet w een these t w o extremes are overriding and deferr e d metho ds . 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 olymor- phism is de�ned in a manner roughly equiv alen t to what w e are here calling overlo ading . In [Sethi 1989 ] and [Mey er 1988a ] and in the functional programming languages comm unit y (suc h as [Wikstr� om 1987 , Milner 1990 ]), the term is reserv ed for what w e are calling pur e p olymorphism . Other authors use the term for one, t w o, or all of the mec hanisms describ ed in this c hapter. Tw o complete, but tec hnically daun ting, analyses are [Cardelli 1985 ] and [Danforth 1988 ]. 207

  2. 208 CHAPTER 12. POL YMORPHISM 12.2 P olymorphic V ariables With the exception of o v erloading, p olymorphism in ob ject-orien ted languages is made p os- sible only b y the existence of and the idea of substitutabilit y . A p olymorphic variables p olymorphic v ariable is one with man y faces; that is, it can hold v alues of di�eren t t yp es. P olymorphic v ariables em b o dy the principle of substitutabilit y . In other w ords, 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 of the exp ected t yp e. In dynamically b ound languages (suc h as Smalltalk), all v ariables are p oten tially p olymorphic{ an y v ariable can hold v alues of an y t yp e. In these languages the desired t yp e is de�ned b y a set of exp ected b eha viors. F or example, an algorithm ma y mak e use of an arra y v alue, exp ecting the subscripting op erations to b e de�ned for a certain v ariable; an y t yp e that de�nes the appropriate b eha vior is suitable. Th us, the user could de�ne his or her o wn t yp e of arra y (for example, a sparse arra y) and, if the arra y op 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 oly- morphism o ccurs in Ja v a through the di�erence b et w een the declared (static) class of a v ariable and the actual (dynamic) class of the v alue the v ariable con tains. A go o d example of a p olymorphic 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 of t yp e Ca rdPile , but in fact it main tains v alues from eac h of the di�eren t sub classes of the paren t class. A message presen ted to a v alue from this arra y , suc h as in the example co de sho wn displa y b elo w, executes the metho d asso ciated with the dynamic t yp e of the v ariable and not that of the static class. public class Solitaire extends Applet f ... static CardPile allPiles [ ]; ... f public void paint(Graphics g) 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 if there are t w o or more function b o dies asso ciated overlo ade d with it. Note that o v erloading is a necessary part of o v erriding, whic h w e and will describ e

  3. 12.3. O VERLO ADING 209 in the next section, but the t w o terms are not iden tical and o v erloading can o ccur without o v erriding. In o v erloading, it is the function name that is p olymorphic{it has man y forms. Another w a y to think of o v erloading and p olymorphism is that there is a single abstract function that tak es v arious t yp es of argumen ts; the actual co de executed dep ends on the argumen ts giv en. The fact that the compiler can often determine the correct function at compile time (in a strongly t yp ed language), and can therefore generate only a single co de sequence are simply optimizations. 12.3.1 Ov erloading Messages in Real Life In Chapter 1 w e sa w an example in whic h o v erloading o ccurred without o v erriding, when I w an ted to surprise m y friend with �o w ers for her birthda y . One p ossible solution w as to send the message sendFlo w ersT o to m y lo cal �orist; another w as to giv e the same message to m y wife. Both m y �orist and m y wife (an instance of class Sp ouse ) w ould ha v e understo o d the message, and b oth w ould ha v e acted on it to pro duce a similar result. In a certain sense, I could ha v e though t of sendFlo w ersT o as b eing one function understo o d b y b oth m y wife and m y �orist, but eac h w ould ha v e used a di�eren t algorithm to resp ond to m y request. Note, in particular, that there w as no inheritance in v olv ed in this example. The �rst common sup erclass for m y wife and m y �orist w as the category Human . But certainly the b eha vior sendFlo w ersT o w as not asso ciated with all h umans. My den tist, for example, who is also a h uman, w ould not ha v e understo o d the message at all. 12.3.2 Ov erloading and Co ercion As an example more closely tied to programming languages, supp ose a programmer is dev el- oping a library of classes represen ting common data structures. A n um b er of data structures can b e used to main tain a collection of elemen ts (sets, bags, dictionaries, arra ys, and priorit y queues, for example), and these migh t all de�ne a metho d, add , to insert a new elemen t in to the collection. This situation{in whic h t w o totally separate functions are used to pro vide seman tically similar actions for di�eren t data t yp es{o ccurs frequen tly in all programming languages, not simply in ob ject-orien ted languages. P erhaps the most common example is the o v erloading of the addition op erator, +. The co de generated b y a compiler for an in teger addition is often radically di�eren t from the co de generated for a �oating-p oin t addition, y et programmers tend to think of the op erations as a single en tit y , the \addition" function. In this example it is imp ortan t to p oin t out that o v erloading ma y not b e the only activit y taking place. A seman tically separate op eration, cion , is also usually asso ciated with c o er arithmetic op erations. It o ccurs when a v alue of one t yp e is con v erted in to one of a di�eren t t yp e. If mixed-t yp e arithmetic is p ermitted, the addition of t w o v alues ma y b e in terpreted in a n um b er of di�eren t w a ys:

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend