<?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>Marius Bancila's Blog &#187; interfaces</title>
	<atom:link href="http://mariusbancila.ro/blog/tag/interfaces/feed/" rel="self" type="application/rss+xml" />
	<link>http://mariusbancila.ro/blog</link>
	<description>Sharing my opinions and ideas!</description>
	<lastBuildDate>Sun, 08 Aug 2010 09:36:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Interface Implementation in C#</title>
		<link>http://mariusbancila.ro/blog/2009/12/08/interface-implementation-in-csharp/</link>
		<comments>http://mariusbancila.ro/blog/2009/12/08/interface-implementation-in-csharp/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 09:46:07 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[interfaces]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=419</guid>
		<description><![CDATA[I recently found a piece of code that can be summarized by the following sample: interface I { void F1(); void F2(); } class X { public void F2() { Console.WriteLine("F2"); } } class A : X, I { public void F1() { Console.WriteLine("F1"); } } As you can see there is an interface I [...]]]></description>
			<content:encoded><![CDATA[<p>I recently found a piece of code that can be summarized by the following sample:</p>
<pre class="prettyprint">
interface I
{
   void F1();
   void F2();
}

class X
{
   public void F2() { Console.WriteLine("F2"); }
}

class A : X, I
{
   public void F1() { Console.WriteLine("F1"); }
}
</pre>
<p>As you can see there is an interface <i>I</i> that has two methods, F1 and F2. <i>A</i> is derived from <i>X</i>, that has a method F2, and also implements I, but only contains F1. I was puzzled at first, because I was expecting that <i>A</i> was <i>explictitly</i> implementing all the methods defined in the interface <i>I</i>. But F2 was implemented in <em>X</em>, its base class. After thinking a little bit it all become clear. This was a normal behavior of the compiler.</p>
<p>When a class <i>A</i> implements an interface <i>I</i> it guarantees that it supports (implements) the entire contract that the interface defines. But it does not assert that it will explicitly implement all the interface members within its explicit definition. I&#8217;m stressing on the <i>explicit</i> word here, because <i>A</i> extends (is derived from) <i>X</i>. That means <i>A</i> <i>is an</i> <i>X</i>. Everything that <i>X</i> exposes (i.e. what is visible to its derived classes) is part of <i>A</i> too. </p>
<p>In our case, F2, implemented in <i>X</i>, is also available to <i>A</i>, because <i>A</i> is an <i>X</i>. Since both F1 and F2 are members of <i>A</i>, then it means <i>A</i> fully implements <i>I</i>, which makes the code compile just fine.</p>
<p>How is this helpful? Suppose you have several interfaces that all define one ore several members with the same meaning. </p>
<pre class="prettyprint">
interface I1
{
  void F1();
  void F2();
  int ErrorCode { get; }
}

interface I2
{
  void G1();
  void G2();
  int ErrorCode { get; }
}

interface I3
{
  void H1();
  int ErrorCode { get; }
}
</pre>
<p>Instead of providing the same implementation several times, like in the following code, you can have only one implementation for the common functionality.</p>
<pre class="prettyprint">
class A : I1
{
  private int m_errorCode;

  public void F1() {}
  public void F2() {}
  public int ErrorCode { get {return m_errorCode;} }
}

class B : I2
{
  private int m_errorCode;

  public void G1() {}
  public void G2() {}
  public int ErrorCode { get {return m_errorCode;} }
}

class C : I3
{
  private int m_errorCode;

  public void H1() {}
  public int ErrorCode { get {return m_errorCode;} }
}
</pre>
<p>We can create one class that provides the implementation for ErrorCode and let the others extend it and implement the corresponding interface.</p>
<pre class="prettyprint">
class X
{
  protected int m_errorCode;

  public int ErrorCode { get {return m_errorCode;} }
}

class A : X, I1
{
  public void F1() {}
  public void F2() {}
}

class B : X, I2
{
  public void G1() {}
  public void G2() {}
}

class C : X, I3
{
  public void H1() {}
}
</pre>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/12/08/interface-implementation-in-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
