<?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>Coding Friends &#187; polymorphism</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/polymorphism/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +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>Polymorphism</title>
		<link>http://www.codingfriends.com/index.php/2010/02/02/polymorphism/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=polymorphism</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/02/polymorphism/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 10:12:48 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=633</guid>
		<description><![CDATA[Polymorphism means that you are deriving from a base class to create a new class, and the polymorphism is when you derive from the base class and implement a interface (virtual function in c++ speak as such) functions. In C++ a virtual function would be virtual void printName&#40;&#41; = 0; the = 0 means to [...]]]></description>
			<content:encoded><![CDATA[<p>Polymorphism means that you are deriving from a base class to create a new class, and the polymorphism is when you derive from the base class and implement a interface (virtual function in c++ speak as such) functions.</p>
<p>In C++ a virtual function would be</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></pre></div></div>

<p>the = 0 means to set the virtual function to point to nothing (pure virtual function).</p>
<p>You could put some code within {} and still call it a virtual function, but that is taking away from the interface idea. (Java,C#  and PHP use the interface structures)</p>
<p>In the code below, I have a const string (constant string) and as we know a constant string cannot be altered so to define this string when the constructor is called you place the variable name after the constructor and set it up to the constant value, e.g..</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Shape
<span style="color: #008000;">&#123;</span>
     <span style="color: #0000ff;">const</span> string name<span style="color: #008080;">;</span>
      <span style="color: #666666;">// constructor</span>
     Shape<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> constructorName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> name<span style="color: #008000;">&#40;</span>constructorName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This will setup the constant string when the Shape class has been created on the heap (memory allocation).</p>
<p>To call a parent class, Shape in this instance, constructor you just do something very similar to the setting of the constant string, you place it after the constructor in the subclass and put the parent class that you want to call its constructor after the &#8220;:&#8221; as</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Rectangle 
<span style="color: #008000;">&#123;</span>
      Rectangle<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> constructorName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> Shape<span style="color: #008000;">&#40;</span>constructorName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>you can place more code into the {} for the Rectangle constructor still, but the &#8220;Shape(constructorName) is calling the parent class &#8220;Shape&#8221; constructor.</p>
<p>If you do not define the virtual pure function within the derived class, when you try to compile the error would be something similar to</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">poly.cpp:<span style="color: #000000;">46</span>: error: cannot allocate an object of abstract <span style="color: #7a0874; font-weight: bold;">type</span> ‘Circle’
poly.cpp:<span style="color: #000000;">21</span>: note:   because the following virtual functions are pure within ‘Circle’:
poly.cpp:<span style="color: #000000;">11</span>: note:      virtual void Shape::printName<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Because the Circle was not implementing what the parent class has defined as a pure virtual function.</p>
<p>In the code below I have placed some error values if you do not put &#8220;public&#8221; in the same places.  These are typical errors that come from access to the objects internals that you are trying to access.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Shape 
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
      Shape<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> conName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> name<span style="color: #008000;">&#40;</span>conName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> 
      <span style="color: #666666;">// create a virtual function so that it is implemented in the subclass</span>
      <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// protected, so it does not allow direct access, but subclasses can access it.</span>
  <span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
      <span style="color: #0000ff;">const</span> string name<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// need to be &quot;public Shape&quot; otherwise</span>
<span style="color: #666666;">// error: ‘Shape’ is an inaccessible base of ‘Circle’</span>
<span style="color: #666666;">// it will not allow access to the Shape class.</span>
<span style="color: #0000ff;">class</span> Circle <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// you need to specify public for Cat to access Animal string name because that is protected</span>
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span>
      Circle <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> conName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> Shape<span style="color: #008000;">&#40;</span>conName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
	  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; class&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Rectangle<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// you need to specify public for Dog to access Animal string name because that is protected</span>
  <span style="color: #666666;">// error: ‘Rectangle::Rectangle(const std::string&amp;)’ is private</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> 
      Rectangle<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> conName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> Shape<span style="color: #008000;">&#40;</span>conName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
	  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; class&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Shape<span style="color: #000040;">*</span> shapes<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    shapes<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Circle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Cirlce&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    shapes<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Rectangle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">2</span> <span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      shapes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #666666;">// clean up the memory space.</span>
      <span style="color: #0000dd;">delete</span><span style="color: #008000;">&#40;</span>shapes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and here is the output</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Cirlce class
Rectangle class</pre></div></div>

<p>So in the example code above, the polymorphism is when for example the Rectangle class is defining the &#8220;interface&#8221; code for printName method.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/02/polymorphism/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overrideing polymorphism &#8211; c#</title>
		<link>http://www.codingfriends.com/index.php/2010/01/15/polymorphism-override-csharp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=polymorphism-override-csharp</link>
		<comments>http://www.codingfriends.com/index.php/2010/01/15/polymorphism-override-csharp/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 11:53:48 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[overloading]]></category>
		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=522</guid>
		<description><![CDATA[Overrideing is also similar to overloading (sometimes it can be called the same thing since you are overloading/polymorphism functions and classes). Polymorphism is when you implement functions that are defined in the base class, overriding is when you over ride a base class function. But with Overrideing classes in c# you can override functions from [...]]]></description>
			<content:encoded><![CDATA[<p>Overrideing is also similar to <a href="http://www.codingfriends.com/index.php/2010/01/14/overloading-methods-c/">overloading</a> (sometimes it can be called the same thing since you are overloading/polymorphism functions and classes). </p>
<p>Polymorphism is when you implement functions that are defined in the base class, overriding is when you over ride a base class function.</p>
<p>But with Overrideing classes in c# you can override functions from the base class that are declared as virtual.  Virtual means that they are capable of being overridden in a inherited class, so that incase someone tries to call a method of a same name in a subclass then the base class is still called e.g.  sometimes better using code and output to show more than what words can say.</p>
<p>Here is not using the virtual keyword in the base class, so that when you try to call the subclasses same method it still goes to the base class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> polymorphism
<span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">class</span> Shape <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Shape base class&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> Circle <span style="color: #008000;">:</span> Shape <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Circle class&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> Rectangle <span style="color: #008000;">:</span> Shape <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Rectangle class&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> Line <span style="color: #008000;">:</span> Shape <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Line class&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> MainClass
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Shape<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> shapesArray <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Shape<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
			shapesArray<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Shape<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			shapesArray<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Circle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			shapesArray<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			shapesArray<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Line<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Shape shape <span style="color: #0600FF; font-weight: bold;">in</span> shapesArray<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				shape<span style="color: #008000;">.</span><span style="color: #0000FF;">printName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>output would</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Shape base class
Shape base class
Shape base class
Shape base class</pre></div></div>

<p>but with the</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #008000;">-</span>  <span style="color: #0600FF; font-weight: bold;">override</span></pre></div></div>

<p>keywords.</p>
<p>The code</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> polymorphism
<span style="color: #008000;">&#123;</span> 
	<span style="color: #6666cc; font-weight: bold;">class</span> Shape <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Shape base class&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> Circle <span style="color: #008000;">:</span> Shape <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Circle class&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> Rectangle <span style="color: #008000;">:</span> Shape <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span>  <span style="color: #6666cc; font-weight: bold;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Rectangle class&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> Line <span style="color: #008000;">:</span> Shape <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span>  <span style="color: #6666cc; font-weight: bold;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Line class&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> MainClass
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Shape<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> shapesArray <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Shape<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
			shapesArray<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Shape<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			shapesArray<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Circle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			shapesArray<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			shapesArray<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Line<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Shape shape <span style="color: #0600FF; font-weight: bold;">in</span> shapesArray<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				shape<span style="color: #008000;">.</span><span style="color: #0000FF;">printName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As expected the printName() function was called from the subclasses, because of the virtual keyword.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Shape base class
Circle class
Rectangle class
Line class</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/01/15/polymorphism-override-csharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

