<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: The dark side of Visual Basic</title>
	<atom:link href="http://pradeepc.net/blog/2005/01/18/the-dark-side-of-visual-basic/feed/" rel="self" type="application/rss+xml" />
	<link>http://pradeepc.net/blog/2005/01/18/the-dark-side-of-visual-basic/</link>
	<description>the colors of my life</description>
	<lastBuildDate>Tue, 07 Feb 2012 11:04:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://pradeepc.net/blog/2005/01/18/the-dark-side-of-visual-basic/comment-page-1/#comment-300</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 19 Jan 2005 06:43:00 +0000</pubDate>
		<guid isPermaLink="false">http://pradeepc.net/blog/?p=34#comment-300</guid>
		<description>These have been discussed in the inheritance vs composition debates. Also, a variant of the Adapter pattern from GoF book uses a techqiue like this.

But, consider a case like this:

public interface Foo {
&#160;&#160;public Foo meth1();
&#160;&#160;public void meth2();
}

public class FooImpl implement Foo {
&#160;&#160;public Foo meth1() {
&#160;&#160;// .. do something
&#160;&#160;return this;
&#160;&#160;}

&#160;&#160;public void meth2() {
&#160;&#160;// .. do something
&#160;&#160;}
}

If I had implementation inheritance, I could do:

public class BarImpl1 extends Foo {
&#160;&#160;public void meth2() {
&#160;&#160;super.meth2();
&#160;&#160;// .. do something extra
&#160;&#160;}
}

If you use composition for this, you end up with:

public class BarImpl2 implements Foo {
&#160;&#160;private Foo f = null; // .. initalized somewhere

&#160;&#160;public void meth2() {
&#160;&#160;f.meth2();
&#160;&#160;// .. do something extra
&#160;&#160;}

&#160;&#160;public Foo meth1() {
&#160;&#160;return f.meth1(); // DICEY!
&#160;&#160;}
}

Bar2 looks OK, except until some client calls:
&#160;&#160;bar.meth1().meth2();

The &quot;extra&quot; stuff you added in Bar2 would not be invoked! In general, to use this technique the base class should not pass itself outside.

In general, I have noticed that,
1. composition is more useful than inheritance
2. interface inheritance is often more appropriate than implementation inheritance

Good Luck on your project!

Cheerio,
Binil :-)

PS: How do I format code, besides resorting to clunky en-bee-es-pee&#039;s? This app needs to provide a help link on comment entry page!</description>
		<content:encoded><![CDATA[<p>These have been discussed in the inheritance vs composition debates. Also, a variant of the Adapter pattern from GoF book uses a techqiue like this.</p>
<p>But, consider a case like this:</p>
<p>public interface Foo {<br />
&nbsp;&nbsp;public Foo meth1();<br />
&nbsp;&nbsp;public void meth2();<br />
}</p>
<p>public class FooImpl implement Foo {<br />
&nbsp;&nbsp;public Foo meth1() {<br />
&nbsp;&nbsp;// .. do something<br />
&nbsp;&nbsp;return this;<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public void meth2() {<br />
&nbsp;&nbsp;// .. do something<br />
&nbsp;&nbsp;}<br />
}</p>
<p>If I had implementation inheritance, I could do:</p>
<p>public class BarImpl1 extends Foo {<br />
&nbsp;&nbsp;public void meth2() {<br />
&nbsp;&nbsp;super.meth2();<br />
&nbsp;&nbsp;// .. do something extra<br />
&nbsp;&nbsp;}<br />
}</p>
<p>If you use composition for this, you end up with:</p>
<p>public class BarImpl2 implements Foo {<br />
&nbsp;&nbsp;private Foo f = null; // .. initalized somewhere</p>
<p>&nbsp;&nbsp;public void meth2() {<br />
&nbsp;&nbsp;f.meth2();<br />
&nbsp;&nbsp;// .. do something extra<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public Foo meth1() {<br />
&nbsp;&nbsp;return f.meth1(); // DICEY!<br />
&nbsp;&nbsp;}<br />
}</p>
<p>Bar2 looks OK, except until some client calls:<br />
&nbsp;&nbsp;bar.meth1().meth2();</p>
<p>The &#8220;extra&#8221; stuff you added in Bar2 would not be invoked! In general, to use this technique the base class should not pass itself outside.</p>
<p>In general, I have noticed that,<br />
1. composition is more useful than inheritance<br />
2. interface inheritance is often more appropriate than implementation inheritance</p>
<p>Good Luck on your project!</p>
<p>Cheerio,<br />
Binil <img src='http://pradeepc.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>PS: How do I format code, besides resorting to clunky en-bee-es-pee&#8217;s? This app needs to provide a help link on comment entry page!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

