<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>djsipe.com &#187; polymorphism</title>
	<atom:link href="http://blog.djsipe.com/tag/polymorphism/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.djsipe.com</link>
	<description>Web Development</description>
	<lastBuildDate>Sat, 31 Dec 2011 16:25:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Plato and Ploymorphism</title>
		<link>http://blog.djsipe.com/2008/01/21/plato-and-ploymorphism/</link>
		<comments>http://blog.djsipe.com/2008/01/21/plato-and-ploymorphism/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 03:44:21 +0000</pubDate>
		<dc:creator>DJ</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://www.djsipe.com/2008/01/21/plato-and-ploymorphism/</guid>
		<description><![CDATA[Until about 3 weeks ago, I had almost no idea what &#8220;polymorphism&#8221; meant&#8212;although I knew exactly how to do it. It&#8217;s one of those things you&#8217;re just going to eventually pick up if you play around with object oriented programming enough, whether you know that&#8217;s what it&#8217;s called or not. I remember bumping into the [...]]]></description>
			<content:encoded><![CDATA[<p>Until about 3 weeks ago, I had almost no idea what &#8220;polymorphism&#8221; meant&#8212;although I knew exactly how to do it.  It&#8217;s one of those things you&#8217;re just going to eventually pick up if you play around with object oriented programming enough, whether you know that&#8217;s what it&#8217;s called or not.  I remember bumping into the term a couple times but the nerd-power behind the lengthy volumes describing it defeated me time and again.  Here&#8217;s stab at a crude explanation in terms I know I could have understood.</p>
<p>The basic premise of <a href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming">polymorphism</a>, one might argue, can be likened to <a href="http://en.wikipedia.org/wiki/Theory_of_forms" target="_blank">Plato&#8217;s theory of Forms</a>.  Plato believed that everything in the world was an imperfect representation of its true Form.  A rock on the ground could be thought of as an inexact approximation of its true Form: &#8220;Rock&#8221;.  Likewise, that chair in your room is merely mimicking the perfect, idealized form of a chair.  So somewhere&#8212;perhaps not in physical form&#8212;there exists a pure, perfect, and authoritative version of a chair that all chairs here on Earth mimic, but never quite get 100% right.</p>
<p>It may sound a bit existential, but what Plato was really talking about (from a computer science point of view) was methods of categorization.  Think about it.  How hard would it be to get a computer to recognize that chair in your room as a chair and not a lamp?  By the same token, it&#8217;s very easy for us to classify it as a chair.  When we classify things on the spot like this, we&#8217;re comparing what we see with our own mental vision of what a chair&#8217;s &#8220;true Form&#8221; is.</p>
<p>What is its <em>true</em> Form?  What makes a chair a chair, or a door a door?  Well, a chair will typically have 3 or more legs, a place to rest your bum, and a back you can lean against.  Doors are harder to pin down, but they are generally embedded in walls, and will open somehow to let you go through that wall.</p>
<p>For now, let&#8217;s take the example of a door and try to point this ship back towards the topic at hand: polymorphism.  Suppose we wanted to build some code to mimic a door.  What would it need to do?  Ideally it would <em>open</em>.  It should probably also be in a wall of some kind.  Some basic height and width dimensions might be handy too.  So, if I may, this might be your pseudo-code for a door:</p>
<p><code>door {<br />
<em style="margin-left: 20px">action:</em> open();<br />
<em style="margin-left: 20px">property:</em> wallType;<br />
<em style="margin-left: 20px">property:</em> width;<br />
<em style="margin-left: 20px">property:</em> height;<br />
}</code></p>
<p>What we just pseudo-created is, arguably, the &#8220;perfect&#8221; Form of a door.  Really, we just created a door that&#8217;s so generalized it&#8217;s pretty much useless.  What we really need is an <em>instance</em> of this door, that we can use and go through:</p>
<p><code>supermarketDoor is-a <em>door</em> {<br />
<em style="margin-left: 20px">action:</em> open(<br />
<span style="margin-left: 40px"><em>action:</em> slideOpen();<span><span style="margin-left: 40px"><span><br />
<span style="margin-left: 20px">);<span><br />
<em style="margin-left: 20px">property:</em> wallType = "glass";<br />
<em style="margin-left: 20px">property:</em> width = "12 feet";<br />
<em style="margin-left: 20px">property:</em> height = "8 feet";<br />
}</span></span></span></span></span></span></code></p>
<p>And that&#8217;s it.  We just polymorphed.  Did you feel it?  We can now use our <em>supermarketDoor</em> object.  But, from a programming point of view, what allows us to use this door is not that we know how it works.  Rather, we can use this door because we know it is a door, and as such has certain characteristics that we know about.  We know this door, or any other door for that matter, will be a predicable variation of our original &#8220;true Form&#8221; of a door.  It will <em>open</em>, it will be in a wall of some type, and it will have height and width dimensions.  The fact that this door <em>slides </em>open isn&#8217;t important, we just want it to open when we tell it to.</p>
<p>So now we can write a quick bit of code that tells a given door to open.   And since we know all doors mimic the Form of a door, we can rest assured that our code will always work&#8212;no matter what kind of door it&#8217;s presented with.  This allows us to create a million different types of doors, and never have to touch our original code that opens a door again.  That&#8217;s the power of polymorphism.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.djsipe.com/2008/01/21/plato-and-ploymorphism/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

