Monday, December 21, 2009

Interfaces

* Interface is the collection of all public members of the class.
* Interface,like classes,defines a set of properties,methods and events.But unlike classes ,interfaces do not provide implementation.
* This is a powerful programming tool because they allow you to separate the definition of objects from their implementation.
* All the members of the interface are by default public and abstract.we need not specify.
* An interface body is limited to declaration for methods,property,indexer.
* Fields,constructor,constants etc cannot be included in an interface.
* Interface members are implicitly public and it is a compiler error to specify the access level explicitly.
* No implementations are allowed in an interface.
* Methods are specified by giving only the return type,name and parameters list followed by a semicolon.


Syntax with example:
interface IFigure
{
int Dimension
{set;get}
double Area();
double Perimeter();
}




Where do we use interface:
* Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.
Interfaces are more flexible than base classes because you can define a single implementation multiple interfaces.



using System;
namespace InterfaceTest1
{
interface CA{
void Foo(); }
interface CB{
void Foo(); }
public class CC:CA,CB
{
//CA members
public void Foo(){
Console.WriteLine("From CA");
}

//CB members
void CB.Foo(){
Console.WriteLine("From CB");
}
}
class Class1
{
[STAThread]
static void Main(string[]args)
{ CC c = new CC();
CA ca; //Interface variable
CB cb; //Interface variable
ca=c; // Explicit implementation
ca.Foo();
cb=c; // Explicit implementation
cb.Foo();
}}}

The output is CA

CB


* Interfaces are better in situations in which you do not need to inherit implementation from a base class.
* Interfaces are useful in cases where you cannot use class inheritance.For example ,structures cannot inherit from classes,
but they can implement interfaces.
An interface represents a contract, in that a class that implements an interace must implement every aspect of that interface exactly as it is defined.



Example:
using System;
namespace InterfaceTEST
{
interface IFigure //INTERFACE
{
int Dimension
set;get}
double Area();
double Perimeter();
}
class Circle:IFigure //CLASS
{
private int mRadius;
public int Dimension{
get{return mRadius;}
set { mradius = value;}}
public double Area(){
return Math.PI*Dimension*Dimension;
}
public double Perimeter(){
return 2* Math.PI*mradius;
}
}
class Square :IFigure //CLASS
{
private int mSide;
public int Dimension{
get{return mside;}
set { mside = value;}}
public double Area()
{return mside * mside;}
public double Perimeter()
{return 4*mside;}
}
class Class1
{

[STAThread]
static void Main(string[]args)
{ //Circle
Circle C = new Circle();
C.Dimension = 10;
Console.WriteLine("Area of circle=" + C.Area());
Console.WriteLine("Perimeter of circle=" + C.Perimeter());
//Square
Square S = new Square();
S.Dimension =5;
Console.WriteLine("Area of square=" + S.Area());
Console.WriteLine("Perimeter of square=" + S.Perimeter());
}
}
}

No comments:

Post a Comment