Terms of Service Privacy Policy Contact Us

leistware.com

Polymorphism

About...


Polymorphism: The occurance of something in different forms or in the actual greek word of many forms. In the object oriented programming world that can mean practically anything so I am going to attempt to explain it in my own words.

CREATED 2012-11-20 22:55:02.0

00-18-0F

UPDATED 2020-11-18 14:31:33.0

Why?


First I am writing this article for two reasons.

One: for all those times I was asked (mostly job interviews) and there wasn't really time to explain it or when I hear the word polymorphisim used in the work place and the meaning is unclear by the context of the sentence. It is a rather complex subject when it all comes down to it.

CREATED 2012-11-20 23:01:33.0

00-18-10

UPDATED 2020-11-18 14:31:26.0

Where?


In 1967, Christopher Strachey came up with the concept of polymorphism. But, to better understand Polymorphisim look at Monomorphism. A momomorphic language is strictly typed. A data type can have one and only one meaning. Which means that functions and procedures have a unique type that can not be violated. So one couldn't return an interface or an ancestor of an object. Therefore in monomorphic systems typing is very straight forward. That is easy to understand. A type can be ONE and ONLY one thing.

CREATED 2012-11-20 23:01:50.0

00-18-11

UPDATED 2020-11-18 14:31:51.0


On the other hand in a polymorphic system a type can be somewhat transparent or represent more than one variation (or so it seems). Even if the language is strongly typed. JavaScript is weakly typed. If a string is set equal to an integer the compiler just changes it. Java, in turn, is strongly typed and would blow up if you tried the same thing. Anyone that has compiled java code for more than 10 minutes knows that.

CREATED 2012-11-20 23:02:00.0

00-18-12

UPDATED 2020-11-18 14:32:07.0


And in loose terms that is what polymorphism is "of many forms" If String is a decendant of object and a string is passed back from a function that is declared to return object that should be ok in a polymorphic system, since string decends from object.

CREATED 2012-11-20 23:02:18.0

00-18-13

UPDATED 2020-11-18 14:32:12.0


According to Luca Cardelli and Peter Wegner, authors of "On Understanding Types, Data Abstraction, and Polymorphism," they have expanded the original polymorphic paridigm of Strachey to include four types in two catagories, ad hoc and universal. Thier observations look like this:


         coercion
        /
-- Ad hoc
        
         overloading

polymorphisim

              parametric
             /
-- Unviversal
             
              inclusive

CREATED 2012-11-20 23:02:47.0

00-18-14

UPDATED 2020-11-18 14:33:16.0

Cohersive Polymorphisim


The first type is coercion which is fairly straight forward. Coercion and overloading are somewhat similar and, again, according to the writings of Cardelli and Wegner the distinction becomes blurred at times. But there is still a distinction. A compiler generally overloads operators to achieve a result - convienence. For example if one was to add an integer to a double the outcome would be a double. The compiler converts the integer to a double. Look at this:

  int i=2;  double x=2.0;  double total = i+x;  

The outcome would be 4.0. Try it it works. It works because the compiler is coerced into converting the int to a double. Converting a double to an integer would cause a loss of percision so it happens the other way around. But the circutry doesn't matter, in fact there are developers that don't even know it's happening. It would appear that the operator + has many forms. Is that polymorphic? That depends... maybe not truly polymorphic, more like a set of monomorphic functions that achieve the same result.

CREATED 2012-11-20 23:03:23.0

00-18-15

UPDATED 2020-11-18 14:33:18.0

Overloading Polymorphism


Anyone that has been coding in a second generation language (even 4th generation for that matter) knows of method overloading. The compiler includes the parameters of a function in the declaration or signature of the method so multiple methods with the same name appear different as long as the parameters are different. A class can have two methods to perform the same task based on different parameter types. An class that contains a list of strings, maybe names, may have two methods to retrieve those names from the list. First a getName(String) method that retrieves a particular name, where getName(int) retrieves a name in a certain position. Also, getName() may retrieve a name in the next position. Are these methods polymorphic? The same function name seems to take different parameters. Is this behavior “of many forms”? Not exactly true polymorphism but more of a smoke screen. The methods themselves are not operating on a wide range of types, meaning the parameters are not changing or being interpreted as different types rather they are different methods, which means they are not executing the same code. Infact, they are executing different code for each situation. Although it appears to be polymorphic it is actually a set of monomorphic functions posing to be polymorphic.

CREATED 2012-11-20 23:04:27.0

00-18-16

UPDATED 2020-11-18 14:33:19.0

Parametric Polymorphism


When a method can operate efficiently on a range of types with similar structure the parameters of the method or function can have different meaning. Unlike overloading, the method actually takes an object that can assume the identity of it's superclass without re-typing it. e.g. Say class C extends or decends from class B which in turn decends from class A. If the signature of a method includes a parameter type of class C but accepts class A and treats it the same as if it were of type class C, that would be true polymorphism. Because the actual parameter passed in can be treated indentical to the superclass and execute the same code as if it were the superclass. Remember “of many forms”. The parameter is actually changing forms from the subclass to the superclass and the same code is being executed on either type... that's polymorphism!

CREATED 2012-11-20 23:05:56.0

00-18-17

UPDATED 2020-11-18 14:33:20.0

Inclusion Polymorphism


Also subtype polymorphism more closely represents object oreiented programming. In this version one could look at objects as belonging to a heirarchy or structure which allows it to belong to many different classes. In inclusion polymorphism, the heirarchy of classes is polymorphism or "of many forms". Being that a class can decend into another class and begin to take other shapes, it is exhibiting polymorphic behavior. While in Parametric polymorphism a function can operate on a range of types of similar structure and treat them similar executing the same code, inclusion polymorphism the type is actually a part of many classes.

CREATED 2012-11-20 23:07:49.0

00-18-18

UPDATED 2020-11-18 14:33:22.0

Conclusion


In retrospect there are two major catagories of polymorphism, ad-hoc and universal. While ad-hoc polymorphism (i.e. coersion and overloading) seems to exhibit polymorphic behavior it is instead monomorphic at the heart. Whereas, universal polymorphism (i.e. parameteric and inclusion) is more the truer form of polymorphism, because it can truely treat a type and a subtype the same.

It would seem that there are a lot of polymorphic labels that aren't really polymorphic at all but rather a set of monomorphic features. On the other hand, is not beuaty in the eyes of the beholder? If one thought it was polymorphic and by all intents and purposes it seemed that way would that not be in itself, polymorphic? It would seem to me. Call it what you want.

So, mentioning the word polymorphism is more like the pickle. A pickle is actually a pickled cucumber but we just call it a pickle. As well, when we talk of polmorphism we are talking of subtype polymorphism. It is the truer version. Sub-type polymorphism more closely resembles inclusion polymorphism and explains object heirarchies.

I've included a link to a very well thoughtout paper on the subject from much bigger minds than mine. Infact most of my information comes from here.

http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf

By the way, the second reason I wrote this paper was to put it in perspective for myself.

CREATED 2012-11-20 23:08:18.0

00-18-19

UPDATED 2020-11-18 14:33:24.0

Knowledge

Page Server: Ithica

DBID: db.wam

©2012 Leistware Data Systems

      Hello anonymous