You are on page 1of 3

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.

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();
}
}
}

Using Methods, Indexers, Properties, and Event


Declarations Within an Interface
Now, we consider the same example so as to explain the use of indexers, properties, and events within
the interface. The code for the interface IIntelligent and the Main class InterfaceApp is changed.

interface IIntelligence
{
/// Method declaration within the interface
bool intelligent_behavior();

/// Indexer declaration within the interface


object this[int index]
{
get;
set;
}
/// Event declaration within an interface
/// testEvent should also be declared as a delegate before
event testEvent IQEvent;

/// Property declaration within an interface


string IQProperty
{
get{}
set{}
}
}

Using "Is" and "As" Operators to Verify the


Implementation of an Interface
Think what will happen if an object attempted to use a class as though the class had implemented a
method that is not defined in it; you will get an exception at runtime. To remove this ambiguity, we use
the "is" keyword to verify that the method exists in the class implementing the given interface.

I will use the same code of the Mammal class above for explaining this. Here, we need to change only the
Main ( ) because we only need to check that our created object is either implementing the interface or
not.

if(human is IIntelligence)

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

In the example case above, when you will run it, it will give a warning that "the given expression is always
of the provided type" and the validity is also been checked twice: one in using the "is" operator and the
other when casting it to the interface type.

Therefore, we can use the "as" operator to cast and check the types more efficiently. The "as" operator
returns an object type and null if the type mismatched whereas the "is" operator returns a Boolean value.
Here, no warning will be generated.

IIntelligence humanIQ = human as IIntelligence;

if(null != humanIQ)

{
humanIQ.intelligent_behavior();
}

Why Use Interfaces?


As a word of closing, I would describe the advantages and importance of using the interfaces provided by
C#; they are as follows:

1. To allow a class to inherit multiple behaviors from multiple interfaces.


2. To avoid name ambiguity between the methods of the different classes as was in the use of
multiple inheritance in C++.
3. To combine two or more interfaces such that a class need only implement the combined result.
The example code for this is provided as a source file to be downloaded.
4. To allow Name hiding. Name hiding is the ability to hide an inherited member name from any
code outside the derived class.

You might also like