<?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>C.M. Jackson .Net &#187; Polymorphism</title>
	<atom:link href="http://www.cmjackson.net/tag/polymorphism/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cmjackson.net</link>
	<description>Web Design, Programming, Tutorials</description>
	<lastBuildDate>Tue, 18 May 2010 22:43:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Understanding Polymorphism</title>
		<link>http://www.cmjackson.net/2009/06/30/understanding-polymorphism/</link>
		<comments>http://www.cmjackson.net/2009/06/30/understanding-polymorphism/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 23:34:50 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Interface]]></category>
		<category><![CDATA[Object]]></category>
		<category><![CDATA[Object Oriented Programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=236</guid>
		<description><![CDATA[Introduction This article will explain what polymorphism is and how it can help you design and reuse code in Object-Oriented Programming.  You&#8217;ll learn what polymorphism is and how to use it.  In the following examples, I&#8217;ll be using Visual Basic .NET, but any language that supports Object-Oriented Programming should be able to use these same [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This article will explain what polymorphism is and how it can help you design and reuse code in Object-Oriented Programming.  You&#8217;ll learn what polymorphism is and how to use it.  In the following examples, I&#8217;ll be using Visual Basic .NET, but any language that supports Object-Oriented Programming should be able to use these same techniques.</p>
<p>If you have not already read the articles on <a href="http://www.cmjackson.net/2009/06/22/understanding-objects/">Understanding Objects</a> and <a href="http://www.cmjackson.net/2009/06/26/understanding-inheritance/">Understanding Inheritance</a>, please check them out as some of the terms and other information explained there will be used in this article.</p>
<h2>What is Polymorphism?</h2>
<p>According to <a href="http://dictionary.reference.com/browse/polymorphism">dictionary.com</a>, the definition of Polymorphism is as follows:</p>
<blockquote><p>3. <em>Biology</em> The occurrence of different forms, stages, or types in individual organisms or in organisms of the same species, independent of sexual variations.</p></blockquote>
<p>So, how does this relate to programming?  We&#8217;ll develop Objects that have different forms of the same <em>&#8220;species&#8221;</em>.  The great thing about Polymorphism is that when you are building your program, you assume you are working with the <em>&#8220;species&#8221;</em> Object, but you actually use an individual <em>&#8220;organism&#8221;</em> Object.  This allows the <em>&#8220;organism&#8221;</em> Objects to be exchanged in the code for one another.</p>
<p>In other words, say we have a Parent Class called Animal and two Child Classes that Inherit from Animal called Cat and Dog.  The Cat and Dog are both Animals, so they will inherit all the properties and methods that an Animal has.  If we wanted to store an array of Cats or Dogs, we could just declare the array to be of type Animal and then we&#8217;d be able to store either Cats or Dogs within the array.  That is Polymorphism.</p>
<h2>Parent Classes and Interfaces</h2>
<p>Both Parent Classes and Interfaces can be used to setup an Object as the <em>&#8220;species&#8221;</em> level.  Parent Classes have been explained in <a href="http://www.cmjackson.net/2009/06/26/understanding-inheritance/">Understanding Inheritance</a>.</p>
<h2>What is an Interface?</h2>
<p>An Object&#8217;s Interface is how you communicate or interact with that Object.  Interfaces in programming terms are similar to a Class Object, but an Interface does not implement the properties or methods it declares, it simply declares them.</p>
<p>Why is this useful?  Interfaces force the Class Objects that implement the Interface to have all of the properties and methods of the Interface.  So, the following IAnimal Interface could be implemented by Cat and Dog to force them to implement the properties, Height and Weight, along with the methods, Eat and Drink.</p>
<pre class="brush: vb;">Public Interface IAnimal

    Property Height() As Integer
    Property Weight() As Integer

    Function Eat(footType As String) As Boolean
    Function Drink(drinkType As String) As Boolean

End Interface</pre>
<p>Interfaces can be used with Parent or Child classes and multiple Interfaces can be used by one Class.  Also, in some cases, you may want to use an Interface in place of a Parent Class, forcing all properties and methods to be fully implemented in each class.  Another thing to keep in mind is that a Parent Class that implements the IAnimal Interface does not have to fully implement all properties and methods.  Instead, the Parent Class could implement those common properties or methods that will not change no matter the Child Class, and leave the others to be implemented by the Child Class.  In VB.NET, you can use the MustOverride keyword on the function declaration.  This will make the Parent Class an Abstract class.</p>
<pre class="brush: vb;">Public MustInherit Class Animal Implements IAnimal

    'Data Members.
    Private _height As Integer
    Private _weight As Integer

    Public Property Height() As Integer Implements IAnimal.Height
        Get
            return _height
        End Get
        Set(ByVal Value As Integer)
            _height = value
        End Set
    End Property

    Public Property Weight() As Integer Implements IAnimal.Weight
        Get
            Return _weight
        End Get
        Set(ByVal Value As Integer)
            _weight = Value
        End Set
    End Property

    'Abstract Functions.
    Public MustOverride Function Eat(foodType As String) As Boolean Implements IAnimal.Eat
    Public MustOverride Function Drink(drinkType As String) As Boolean Implements IAnimal.Drink

End Class</pre>
<p> Now, things are looking pretty good.  We can force are Classes to implement certain properties and methods, but there is still one downside.  Our methods are passing in a String value for foodType and drinkType.  What if we wanted to use a different data type, such as our own Object that holds other properties for foodType?  We don&#8217;t want to have to modify the Interface or Parent Class each time this may change.  What we want to do is make the Class or Interface Generic.</p>
<h2>How do I make my Interface or Class Generic?</h2>
<p>Making a Class or Interface Generic will make your code more reusable.  When you make a Class or Interface Generic, you don&#8217;t have to know what type of data you will be using with the Object until you declare or create an instance of the Object.  So, various programs could use the same Class Object, but passing in different data types to work with when instantiating the Object. </p>
<pre class="brush: vb;">Public Interface IAnimal(Of T)

    Property Height() As Integer
    Property Weight() As Integer

    Function Eat(foodType As T) As Boolean
    Function Drink(drinkType As T) As Boolean

End Interface</pre>
<p>T is a place holder for the type of data we will declare later on.  Notice in the declaration of the Interface and both methods, Eat and Drink, T is used.  Now, when we create a Cat or Dog using the IAnimal Interface, we can determine what type of data will be used for foodType and drinkType.</p>
<pre class="brush: vb;">Public Class Cat Implements IAnimal(Of String)</pre>
<p>This would declare a Cat to use a String data type to store the foodType and drinkType.</p>
<pre class="brush: vb;">Public Class Dog Implements IAnimal(Of Integer)</pre>
<p>And this one would declare a Dog to use an Integer data type to store the foodType and drinkType.</p>
<p>We could also define our own complex Class Object as the data type.</p>
<h2>Advantages of using Polymorphism, Interfaces, and Generic Objects</h2>
<ul>
<li>Polymorphism allows you to use similar types of data together.  It also can be used to improve system maintenance, reuse code, and easily setup testing Objects that mimic communications with databases and other resources.</li>
<li>Interfaces force the implementation of properties and methods on Objects that implement the Interface.</li>
<li>Generic Objects allow more reusable code when building Class Objects and Interfaces.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2009/06/30/understanding-polymorphism/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
