<?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>Fremus.co.za &#187; OOP</title>
	<atom:link href="http://www.fremus.co.za/blog/tag/oop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fremus.co.za/blog</link>
	<description>Demistifying Life and Web Development</description>
	<lastBuildDate>Sun, 27 Nov 2011 15:56:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>C# OOP Concepts, Polymorphism</title>
		<link>http://www.fremus.co.za/blog/2009/01/c-oop-concepts-polymorphism/</link>
		<comments>http://www.fremus.co.za/blog/2009/01/c-oop-concepts-polymorphism/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 10:36:53 +0000</pubDate>
		<dc:creator>fr3dr1k</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Encapsulation]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Polymorphism]]></category>

		<guid isPermaLink="false">http://www.fremus.co.za/blog/?p=320</guid>
		<description><![CDATA[There are three very basic OO concepts that apply to most OO programming languages today: Inheritance / Generalization / Specialization Encapsulation Polymorphism The concept I am going to discuss here is polymorphism. Polymorphism as a concept can be taken from the concept of encapsulation, which states that an object is a complete thing that hides [...]]]></description>
			<content:encoded><![CDATA[<p>There are three very basic OO concepts that apply to most OO programming languages today:</p>
<ul>
<li>Inheritance / Generalization / Specialization</li>
<li>Encapsulation</li>
<li>Polymorphism</li>
</ul>
<p>The concept I am going to discuss here is <strong>polymorphism</strong>. <strong>Polymorphism</strong> as a concept can be taken from the concept of <strong>encapsulation</strong>, which states that an <strong>object</strong> is a complete thing that hides itself from other objects. Thus the other objects that have a relationship (possibly <strong>inheritance</strong>) with that object do not get to see the implementation details of the members from that object. In its simplest form <strong>polymorphism</strong> basically takes a method from an inheriting class, uses the same method name, but implements it in its own way. </p>
<p>A good analogy of this concept is to think of a telephone company that rents out telephone lines. The telephone company will send calls through to your line without knowing how your telephone device will react to that phone call. All the telephone company knows is that it needs to send the call through to your line, and the details of how you handle it is completely hidden. Your ring tone could be very different from a neighbours&#8217; ring tone. I got this analogy from <a href="http://www.kalahari.net/books/Programming-C-3-0-None/632/32353941.aspx" onclick="urchinTracker('/outgoing/www.kalahari.net/books/Programming-C-3-0-None/632/32353941.aspx?referer=');">Jesse Liberty&#8217;s book, Programming C# 3.0</a>, 5th Edition and I thought it explains two important <strong>OO concepts</strong>, <strong>encapsulation</strong> and <strong>polymorphism</strong> very well.</p>
<p>Polymorphism is implemented in C# by using the <code>virtual</code> keyword in the parent class and by using the <code>override</code> keyword in the child class:</p>
<pre name="code" class="csharp">
namespace UsingVirtualMethods2
{
    class Program
    {
        static void Main(string[] args)
        {
            TelephoneMessage myMsg = new TelephoneMessage();
            TelephoneReceiver myRec = new TelephoneReceiver();
            myMsg.InterpretMessage();
            myRec.InterpretMessage();
        }
    }
    class TelephoneMessage
    {
        public virtual void InterpretMessage()
        {
            Console.WriteLine("This is the message coming through");
        }
    }
    class TelephoneReceiver : TelephoneMessage
    {
        public override void InterpretMessage()
        {
            Console.WriteLine("I have received your message and am interpreting it");
        }
    }
}
</pre>
<p>From the code above you can see that I use the <code>virtual</code> keyword in the <code>TelephoneMessage</code> class and then <code>override</code> it in the <code>TelephoneReceiver</code> class. <code>TelephoneReceiver</code> inherits from <code>TelephoneMessage</code>. I then create two instances of each class in the main method and call the <code>InterpretMessage()</code> method for each instance. This simple example actually illustrates basic usage of the three concepts mentioned earlier. usingvirtualmethods2</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save" onclick="urchinTracker('/outgoing/www.addtoany.com/share_save?referer=');"><img src="http://www.fremus.co.za/blog/wp-content/plugins/add-to-any/favicon.png" width="16" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.fremus.co.za/blog/2009/01/c-oop-concepts-polymorphism/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

