Monday, March 7, 2011

Interfaces in C#


Introduction

When hearing the name "Interface," one can easily guess that it's some sort of communicating media or joining line between two other mediums, namely classes here. Although the concept of interfaces in C# resembles this concept, there are few things to be made clear about this keyword and the concept behind it so that the C# programmer can best utilize its availability and can make the code more powerful.
Interfaces in C# are provided as a replacement of multiple inheritance. Because C# does not support multiple inheritance, it was necessary to incorporate some other method so that the class can inherit the behavior of more than one class, avoiding the problem of name ambiguity that is found in C++. With name ambiguity, the object of a class does not know which method to call if the two base classes of that class object contain the same named method.
The classes in C# can now make use of the keyword "interface" to inherit more than one behavior from different interfaces. When a class inherits from one or more interfaces, we say that the class is implementing that interface(s). The most important thing to remember about interfaces is that the classes can only implement the methods defined in the interface because in C#, an interface is a built-in keyword that declares a reference type that includes method declarations. In addition to methods, interfaces can define properties, indexers, and events that will be discussed later in this article.

Implementing Interfaces

First, we consider the following example that will clear the concept more. As we know that Mammals have both similar and dissimilar characteristics, so in this example, we have taken two sub-classes of Mammal: Human and Whale. Because Human is the only subclass that has the characteristic of intelligence that distinguishes it from the other subclasses of Mammal, the Human class inherits both the class Mammal and an interface IIntelligent that selectively describes it as separated from the other classes of Mammal.
interface.cs
/// Implementing interfaces

using System;

namespace ConsoleApplication1
{
public class Mammal
{
protected string Characteristis;
public string characteristics
{
get
{
return this.Characteristis;
}
set 
{
this.Characteristis=value;
}
}
}
interface IIntelligence
{
/// Interface method declaration
bool intelligent_behavior();
}

class Human: Mammal, IIntelligence
{
public Human()
{
characteristics = "Human are mammals";
}

/// Interface method definition in the class that implements it
public bool intelligent_behavior()
{
Console.WriteLine("{0} and have intelligence",characteristics);
return true;
        }
}
class Whale: Mammal
{
public Whale()
{
characteristics = "Whale are mammals";
Console.WriteLine("{0}",characteristics);
}
}


class InterfaceApp
{
public static void Main(string[] args)
{
Whale whale = new Whale();

Human human = new Human();

///The human object is casted to the interface type
IIntelligence humanIQ = (IIntelligence)human;
humanIQ.intelligent_behavior();

Console.Read();
}
}

}

combineinterfaces.cs

/// Combining interfaces

using System;

namespace ConsoleApplication1
{
public class Mammal
{
protected string Characteristis;
public string characteristics
{
get
{
return this.Characteristis;
}
set 
{
this.Characteristis=value;
}
}
}
interface IIntelligence
{
/// Interface method declaration
bool intelligent_behavior();
}
interface Ithinking
{
///Interface method declaration
bool thinking_behavior();
}
interface IAbility : IIntelligence, Ithinking
{
///This interface does not have any methods.
///It just combines two interfaces of IIntelligence and Ithinking
}

class Human: Mammal, IAbility
{
public Human()
{
characteristics = "Human are mammals";
}

/// Interface method definition in the class that implements it
public bool intelligent_behavior()
{
Console.Write("{0}. They have intelligence,",characteristics);
return true;
        }
public bool thinking_behavior()
{
Console.Write("and can also think!");
return true;
}

}
class Whale: Mammal
{
public Whale()
{
characteristics = "Whale are mammals";
Console.WriteLine("{0}",characteristics);
}
}


class InterfaceApp
{
public static void Main(string[] args)
{
Whale whale = new Whale();

Human human = new Human();

///The human object is casted to the interface type
IAbility humanIQ = (IAbility)human;
humanIQ.intelligent_behavior();
humanIQ.thinking_behavior();
Console.Read();
}
}

}

indexers.cs

/// Using indexers in the interface

using System;
using System.Collections;

namespace ConsoleApplication1
{
public class Mammal
{
protected string Characteristis;
public string characteristics
{
get
{
return this.Characteristis;
}
set 
{
this.Characteristis=value;
}
}
}
interface IIntelligence
{
///Method declaration with in the interface 
bool intelligent_behavior();

///Indexer declaration with in the interface
object this[int i]
{
get;
set;
}
}
class Human: Mammal, IIntelligence
{
protected ArrayList human_names=new ArrayList();

/// Constructor for the Human class
public Human()
{
characteristics = "Human are mammals";
}

/// Definition of the indexer 
public object this[int i]
{
get
{
if(i > -1 && i < human_names.Count)
return(human_names[i]);
else
return null; 
}
set
{
if(i > -1 && i <human_names.Count)
human_names[i]= value;
else
{
if(i==human_names.Count)
human_names.Add(value);
}
}
}

/// Definition of the method
public bool intelligent_behavior()
{
Console.WriteLine("{0} and have intelligence",characteristics);
return true;
}
}
class Whale: Mammal
{
public Whale()
{
characteristics = "Whale are mammals";
Console.WriteLine("{0}",characteristics);
}
}

class InterfaceApp
{
public static void Main(string[] args)
{
Whale whale = new Whale();
Human human = new Human();

IIntelligence humanIQ = (IIntelligence)human;
humanIQ.intelligent_behavior();

humanIQ[0]="Martha";
humanIQ[1]="David";

Console.WriteLine("The examples of human are: {0},{1}", humanIQ[0],humanIQ[1]);
Console.Read();
}
}

}




No comments :

Post a Comment