<?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>zedia flash blog &#187; polymorphism</title>
	<atom:link href="http://www.zedia.net/tag/polymorphism/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zedia.net</link>
	<description>Flash, ActionScript, SEO and everything in between</description>
	<lastBuildDate>Thu, 02 Feb 2012 17:58:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to use interface in ActionScript 3</title>
		<link>http://www.zedia.net/2008/how-to-use-interface-in-actionscript-3/</link>
		<comments>http://www.zedia.net/2008/how-to-use-interface-in-actionscript-3/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 00:08:03 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Actionscrip 3]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://www.zedia.net/2008/how-to-use-interface-in-actionscript-3/</guid>
		<description><![CDATA[There are a lot of good ActionScript 3 books out there, but in most of them all the examples about inheritance, composition, interface, polymorphism and more complex object oriented principles are done using data classes. It&#8217;s good to explain the principles, but when you are ready to code some actual classes in Flash, some problems [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of good ActionScript 3 books out there, but in most of them all the examples about inheritance, composition, interface, polymorphism and more complex object oriented principles are done using data classes. It&#8217;s good to explain the principles, but when you are ready to code some actual classes in Flash, some problems start to arise. Most of my problems come from the fact that 90% of the time I am coding classes that are a visual representation of something. So these classes extends the Sprite or MovieClip class.</p>
<p>So I was coding this game class where I have multiple kind of monsters but I want to use the same functions without checking what kind of monster it is. This is a case where polymorphism comes in handy. The thing is I didn&#8217;t do my classes of monster using inheritance (which maybe I should have done), so in order to use polymorphism, I have to create an interface with the public methods common to all my monster classes. So I open the <a href="http://www.moock.org/" title="Colin Moock" target="_blank">Colin Moock</a> book and start looking at how to code interfaces. Here is a list of quick facts that will help you while doing so.</p>
<ul>
<li>You have to list all public methods that will be common for the classes that implements this interface;</li>
<li>You do so by writing the definition of the function, its name, its parameters and its return type;</li>
<li>You don&#8217;t have to open and close curly braces after the method definition</li>
<li>You don&#8217;t specify if the method is private, public or protected (that&#8217;s kind of obvious but I did that mistake)</li>
<li>interface can only extend other interfaces</li>
<li>a class can implement more than one interface</li>
</ul>
<p>So here is an example of an interface in As3:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">mutantfarm</span>.<span style="color: #006600;">monster</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IMonster <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> getShot<span style="color: #66cc66;">&#40;</span>damage:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
    <span style="color: #000000; font-weight: bold;">function</span> getCanShoot<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Here is how my visual class implements this interface while also extending the MovieClip class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> spriteMouton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #0066CC;">implements</span> IMonster<span style="color: #66cc66;">&#123;</span></pre></div></div>

<p>It is important that you put the extend keyword before the implement keyword.</p>
<p>So most of what I just wrote about was details about interfaces, but when you start coding, all these details are important to know.</p>
<p>Here are two more article I wrote about interfaces:</p>
<ul>
<li><a href="http://www.zedia.net/2008/another-good-use-of-interfaces/" title="Another good use of interfaces">Another good use of interfaces</a></i>
<li><a href="http://www.zedia.net/2008/external-preloader-more-complex-cases/" title="External Preloader; more complex cases">External Preloader; more complex cases</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2008/how-to-use-interface-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
	</channel>
</rss>

