Learn how to work with Interfaces using C#

In order to rectify the drawback of multiple inheritance, creators of C# have introduced a new concept called Interfaces. Java programmers may be well aware of this concept.

All interfaces should have to be declared with the keyword interface. You can implement any number of interfaces in a single derived class. But you should provide signatures to all method definitions of the corresponding interfaces. To illustrate, Listing 1 shows how to declare interfaces and implement them in a class:

Listing 1

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
interface Interdemo 
{
void Show(); 
} 
class Interimp:Interdemo 
{ 
public void Show() 
{ 
Console.WriteLine(&quot;Show() method Implemented&quot;); 
} 
public static void Main(string[] args) 
{ 
Interimp inter = new Interimp(); 
inter.Show(); 
}
}


Combining Interfaces

Two or more interfaces can be combined into a single interface and implement in a class as shown in Listing 2:

Listing 2

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
interface Interdemo 
{ 
void Show(); 
}
interface Interdemo1 
{
void Display(); 
} 
interface Combineinter:Interdemo,Interdemo1 
{ 
//Above interfaces combined 
} 
class Multipleinterimp:Combineinter 
{ 
public void Show() 
{ 
Console.WriteLine(&quot;Show() method Implemented&quot;); 
} 
public void Display() 
{ 
Console.WriteLine(&quot;Display() method Implemented&quot;); 
} 
public static void Main(string[] args) 
{ 
Multipleinterimp inter = new Multipleinterimp(); 
inter.Show();
inter.Display();
}
}

You can easily determine whether a particular interface is implemented in a class by using is and as operators. is operator enables you to check whether one type or class is compatible with another type or class and it returns a Boolean value. Listing 3 illustrates the usage of is operator by revisiting Listing 2.

Listing 3

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
interface Interdemo 
{
bool Show(); 
}
interface Interdemo1 
{
bool Display(); 
} 
class Interimp:Interdemo 
{ 
public bool Show() 
{ 
Console.WriteLine(&quot;Show() method Implemented&quot;); 
return true;
} 
public static void Main(string[] args) 
{ 
Interimp inter = new Interimp(); 
inter.Show(); 
if(inter is Interdemo1) 
{
Interdemo1 id = (Interdemo1)inter; 
bool ok = id.Display(); 
Console.WriteLine(&quot;Method Implemented&quot;); 
}
else
{ 
Console.WriteLine(&quot;Method not implemented&quot;); 
}
}
}

While the is operator returns a boolean value as shown in the above listing, as operator returns null if there is any incompatibility between types. Listing 4 examines the usage of this operator. Here we have revisited Listing 3. Notice the change in code inside the Main () method.

READ  Understanding Programming Constructs using C#

Listing 4

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
interface Interdemo 
{ 
bool Show(); 
}
interface Interdemo1 
{
bool Display(); 
} 
class Interimpas:Interdemo 
{
public bool Show() 
{ 
Console.WriteLine(&quot;Show() method Implemented&quot;); 
return true; 
} 
public static void Main(string[] args) 
{ 
Interimpas inter = new Interimpas(); 
inter.Show(); 
Interdemo1 id = inter as Interdemo1; 
if(null!=id) 
{ 
bool ok = id.Display(); 
Console.WriteLine(&quot;Method Implemented&quot;); 
} 
else 
{ 
Console.WriteLine(&quot;Method not implemented&quot;); 
}
}
} 

Avoiding Name Ambiguity

Suppose you are declaring same method definitions in two different interfaces. The compiler will naturally show an error due to ambiguity of the implemented method. Even if you use “is” keyword, compiler will still show warnings. In order to avoid this, you have to follow syntax like as shown below:

Listing 5

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">Void <span style="color: #0000ff">&lt;</span><span style="color: #800000">interface</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">&gt;</span>.<span style="color: #0000ff">&lt;</span><span style="color: #800000">method</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">&gt;</span> 
{ 
//Body goes here 
} 

The listing given below illustrates the application of above concept in detail:

Listing 6

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
interface Interdemo 
{ 
void Show(); 
} 
interface Interdemo1 
{
void Show(); 
}
class Interclash:Interdemo,Interdemo1 
{ 
void Interdemo.Show() 
{ 
Console.WriteLine(&quot;Show() method Implemented&quot;); 
}
void Interdemo1.Show() 
{ 
Console.WriteLine(&quot;Display() method Implemented&quot;); 
} 
public static void Main(string[] args) 
{ 
Interclash inter = new Interclash(); 
inter.Interdemo.Show(); 
inter.Interdemo1.Show(); 
}
}



Leave a Reply

Your email address will not be published.