<?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; interface</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/interface/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-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=polymorphism-2</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/02/polymorphism-2/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 11:09:36 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[interface]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=639</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 functions, so that any derived class will have to these functions implement so if you call a function that is defined by the interface 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 functions, so that any derived class will have to these functions implement so if you call a function that is defined by the interface to be present, you know it will be implement in some forum.</p>
<p>In php, there is a interface type, so we can define a basic Animal type to print is name, so that any animal that is derived from this interface will have to implement at least the print name function, here is the interface</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Animal
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>it is very similar to a class structure apart from there is no functional code, just the function definition.  To then implement the interface Animal within a class, so that you will know that the printName() function will be implemented you use the &#8220;implements&#8221; keyword in the class definition as below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Cat implements Animal
<span style="color: #009900;">&#123;</span><span style="color: #339933;">....</span></pre></div></div>

<p>and then the class Cat will have to define the printName function as</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Cat class<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>otherwise if you did not implement it there would be a error on the &#8220;compile time&#8221; as below.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Fatal error: Class Cat contains <span style="color: #000000;">1</span> abstract method and must therefore be declared abstract or implement the remaining methods <span style="color: #7a0874; font-weight: bold;">&#40;</span>Animal::printName<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>here is a full code that will do a Cat and Dog class, then you can create a array of different derived Animals interfaces and then just call the printName and you know it will be present.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">interface</span> Animal
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Cat implements Animal
<span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Cat class<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Dog implements Animal 
<span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Dog class<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$animals</span> <span style="color: #339933;">=</span> <span style="color: #990000;">Array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Cat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>and the output would be</p>

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

<p>Polymorphism is great, because you just know that certain functions will be implemented.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/02/polymorphism-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
	</channel>
</rss>

