<?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>Schauderhaft &#187; inheritance</title>
	<atom:link href="http://blog.schauderhaft.de/tag/inheritance/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Sun, 25 Jul 2010 14:04:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mixins, Inheritance and Delegation</title>
		<link>http://blog.schauderhaft.de/2010/01/24/mixin-inheritance-delegation/</link>
		<comments>http://blog.schauderhaft.de/2010/01/24/mixin-inheritance-delegation/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 11:27:49 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mixin]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[trait]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=385</guid>
		<description><![CDATA[A week ago I started learning Scala. One of the features I found pretty interesting are mixins and traits. That was just the point of time, when I read this little tweet of GeekyL:
&#8220;i am still not sure if mixins are super cool or dark magic.&#8221;
Of course I was instantly reminded of the time when [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_391" class="wp-caption alignright" style="width: 310px"><a href="http://blog.schauderhaft.de/wp-content/uploads/2010/01/962589_79025341x.jpg"><img class="size-medium wp-image-391" title="962589_79025341x" src="http://blog.schauderhaft.de/wp-content/uploads/2010/01/962589_79025341x-300x299.jpg" alt="Blender" width="300" height="299" /></a><p class="wp-caption-text">Blender</p></div>
<p>A week ago I started learning <a href="http://www.scala-lang.org/">Scala</a>. One of the features I found pretty interesting are <a href="http://www.scala-lang.org/node/117">mixins and traits</a>. That was just the point of time, when I read this little <a href="http://twitter.com/GeekyL/status/8037135496">tweet</a> of <a href="http://twitter.com/GeekyL">GeekyL</a>:</p>
<p>&#8220;i am still not sure if mixins are super cool or dark magic.&#8221;</p>
<p>Of course I was instantly reminded of the time when the dinosaurs dominated the world and I was learning the first little bits of OO and C++. I thought inheritance and polymorphism was great and the solution for every possible programming problem there is. It turned out that was not the case. Actually inheritance can result in pretty ugly code.</p>
<p>So GeekyL&#8217;s tweet got me thinking: Do mixins have the same problem? What exactly are the problems with inheritance anyway? Time for a new blog post.</p>
<p><strong>What are the problems of inheritance?</strong></p>
<p><strong>Inheritance for code reuse</strong>: It is tempting to have a class inherit from a superclass just because the superclass has some useful feature. Like this.</p>
<div class="geshi no java">
<div class="head">import java.beans.PropertyChangeSupport;</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> PropertySupport <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">protected</span> <span class="kw3">PropertyChangeSupport</span> propSup<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> UserEntity <span class="kw2">extends</span> PropertySupport <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">private</span> <span class="kw3">String</span> firstName<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> setFirstName<span class="br0">&#40;</span><span class="kw3">String</span> aFirstName<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">String</span> oldValue = firstName<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; firstName = aFirstName<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; propSup.<span class="me1">firePropertyChange</span><span class="br0">&#40;</span><span class="st0">&quot;firstName&quot;</span>, oldValue, firstName<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The problem with such code is that inheritance suggests a is-a-relationship, which in this case is just wrong. A UserEntity is not a PropertySupport, it just uses one.</p>
<p><strong>Multiple inheritance</strong>: At least in java a class can only inherit from one superclass. But many classes are many things. For example, a Cat is a Carnivor, it is a FourLeggedAnimal and a FurryAnimal. Without multiple inheritance there is just no way how to model this with class inheritance. With multple inheritance you can extend all these classes at the same time, but now you inherit the top class Animal three times, which at least is ugly.</p>
<p>So the proper way to do this kind of stuff in java is to use delegation and interfaces:</p>
<div class="geshi no java">
<div class="head">public class Cat implements Furry, FourLegged {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;FurryDelegate fd<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;FourLeggedDelegate fld<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> pet<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; fd.<span class="me1">pet</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> runFast<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; fld.<span class="me1">runFast</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Which works in the sense that you can pretty much express the kind of things you need to do, and which you might be tempted to solve with class inheritance. Of course the draw back is that you&#8217;ll have to create all theses trivial delegate methods.</p>
<p>In Scala you have the option to use Traits. Like so</p>
<div class="geshi no scala">
<div class="head">class Animal</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class Cat extends Animal with Furry with FourLegs{
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">trait Furry{
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;def pet() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; println(&quot;purrrrr&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">trait FourLegs{
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;def runFast() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; println(&quot;I&#39;m gone&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">}</div>
</li>
</ol>
</div>
<p>Obviously this contains much less code duplication then the java version. Does it have drawbacks? You bet.</p>
<p>Mixins are firmly tied to the traits they use. Imagine a more complex trait, which itself uses many other clases and resources. Once you mix in such a trait you have a strong dependency. Therefor I suggest the following pattern for getting the reduction of code duplication from traits and the flexibility of delegates: Use Traits, which don&#8217;t have a real implementation, but use a delegate for doing all the real stuff, this way you can switch the implementation with touching just a single place:</p>
<div class="geshi no scala">
<div class="head">class Superclass</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class Mixin extends Superclass with Trait {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; // &#8230; more stuff goes here
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">trait Trait {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; val delegate = new TraitImpl // this should be some kind of DI or lookup in a real system
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; def aMethod() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; delegate.aMethod()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class TraitImpl extends Trait {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; override def aMethod(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; println(&quot;you just called me&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">}</div>
</li>
</ol>
</div>
<p>Summary: Traits can be used to solve some of the problems of inheritance, but they might introduce strong coupling, which you can easily be avoided.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/01/24/mixin-inheritance-delegation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
