D'Arcy from Winnipeg
Solution Architecture, Business & Entrepreneurship, Microsoft, and Adoption

Defining Polymorphism

Friday, December 28, 2007 2:56 PM

I'm working on some OO course content for my client, and I'm writing up the part on Polymorphism. Do you realize how difficult it is to put Polymorphism into words?!

I came up with this so far:

"The ability of objects to expose identical access while differing in implementation."

I think that sums it up...but it doesn't really explain it...obviously I'll have some object lessons (ha, get it...object lessons! ha ha...hrm) to try and flesh this out a bit more. But if you were going to sum up Polymorphism how would you do it?



# re: Defining Polymorphism

Taken from "Head First Object Oriented Design and Analysis" by Brett McLaughlin, Gary Pollice and David West:

Polymorphism is closely related to inheritance. When one class inherits from another, then polymorphism allows a subclass to stand in for a superclass.

12/28/2007 4:09 PM | Kelly

# re: Defining Polymorphism

It's been interesting researching this stuff to see how uber-religious the conversation around it really is! Interesting that Pollice and West talk about Polymorphism beign "closely related" to inheritance...where others will say that inheritance is one way to express polymorphism.

Thanks for the comment Kelly :)

D 12/28/2007 4:21 PM | D'Arcy from Winnipeg

# re: Defining Polymorphism

Inheritance is only one way to express polymorphism. Two classes implementing the same interface are polymorphic as well, but possibly only in part.

In defining the term, I'd avoid using 'implementing' and 'interface' as both have overloaded definitions (i.e. interface being either what is exposed or the programming construct). With that in mind I'd probably come up with something like:

"When two or more objects that expose identical outside access to themselves"

I dunno if that works or not...I kinda made it up on the fly here. 12/28/2007 5:42 PM | Donald Belcham

# re: Defining Polymorphism

I usually think of it as "two or more items that can act on the same request -- or set of requests" (but I imagine that's a little "SmallTalky")

Not quite "is-a" or "has-a", but "acts like", as in "this acts like a file -- it can be saved, opened, etc." Different file types may respond to the same set of messages, but they may respond in different ways. 12/28/2007 6:15 PM | Kent Sharkey

# re: Defining Polymorphism

Try to the Liskov Substitution Principle. Defines it quite nicely I think, also has a nice techy definition :)

"If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. [Liskov88]"

Here's the Wikipedeia link

12/29/2007 9:48 AM | Shaneo

# re: Defining Polymorphism

And to put that in plain english as it applies to Polymorphism....

Basically any progam P that is expecting to use a class of type T can be passed an object of type S which is a subclass of T, and P will be unaffected by (and oblivious to) the use of S.
12/29/2007 10:31 AM | Shaneo

# re: Defining Polymorphism

Kent's explanation is a good description that most should be able to understand even if they aren't a developer...expand on it with the classic (at least from my Java days) animal object example...a cat object might have a function "Speak" which would of course give a different result than a dog object's "Speak" function. 12/29/2007 10:41 PM | Lou

Post a comment