Interfaces


An Interface is a named collection of semantically similar abstract members or functions. A class or a structure may follow a behaviour that is prescribed by an interface. The keyword interface is used to create an interface. Note that while defining members inside an interface, there is no need to specify the access specifier, as they are public by default. Interface's members can be Events, Methods, Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can't contain constants, data fields, constructors, destructors and static members. 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.


Why Use Interfaces?

  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.
  4. To allow Name hiding. Name hiding is the ability to hide an inherited member name from any code outside the derived class.

How to implement an interface in C#?

An interface may be implemented using the keyword "implements" in VB.NET
and using a colon in C#. See code C# example below:

interface IBooks
{
long int Cost { get; } //Interface property
}

//ComputerBooks is a class that implements an Interface IBooks

public class ComputerBooks : IBooks

{

// Implementation of the property

public long int Cost
{
get { return 100; } //Read only value
}

}
How to declare and implement an interface?
Interfaces can be declared with the keyword interface and can be implemented in a class
using System;
interface Interdemo
{
void Show();
}

class Interimp:Interdemo
{
public void Show()
{
Console.WriteLine("Show() method Implemented");
}

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 implemented in a class

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("Show() method Implemented");
}

public void Display()
{
Console.WriteLine("Display() method Implemented");
}

public static void Main(string[] args)
{
Multipleinterimp inter = new Multipleinterimp();
inter.Show();
inter.Display();
}
}
Using Methods, Indexers, Properties, and Event Within an Interface
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();
}
Multiple Inheritance using C# interfaces
This can be done using child class that inherits from any number of c# interfaces. The inheritance can also happen with a combination of a C# .Net class and c# interfaces. Now let us see a small piece of code that demonstrate us multiple inheritance using only interfaces as parent data types.

    class ClonableNode : INode,ICloneable
{
public object Clone()
{
return null;
}
// INode members
}
The above example created a class ClonableNode. It implements all the functionality of INode interface in the same way as it was done in Node class. Also it realizes Clone method only one item of IClonable interface of .NET library.

0 comments:

Template by - Mathew | Mux99