Interface---->> If a class contain all abstract function then it is known as an interface.Interface do not provide Implementation.They are implemented by the class and defined as seprate entities from class. An Interface represent a contract in that a class that implements an interface must implement every aspect of that interface exactly as it is defined.
In general Interface provides control over the class. we can define feature as small groups of closly related members and can developed enhanced implementation for your interfaceswithout jeoparadizing existing code,thus minimizing compitability problems.
It is also possible to add new feature to any time by developing adding additional interfaces and implementations.Although intrface implementations can evolve, interfaces themselves cannot be changed once published.
An Interface can contain
1) Abstract functions
2)prperties
3)Indexes
4)Events
An Interface can not contain
1)Non-abstract functions
2)constructor
3)deconstructor
4)Datafields
By default interface functions are treated as public and abstract.An Interface can not be instantiated directly. Creating/deriving a new class or other interface from an Interface is compulsary/mandatory in order to provide implementation to abstract function. An interface can itself inherit from multiple interfaces.An Interface is used to implement Multiple inheritance in C#.Net.
Program to implement Interface---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interface
{
interface Employee
{
void GetEmpData();
void DispalyEmpData();
}
class Manager : Employee
{
int EmpId;
string EName;
double Bonus, CA;
public void GetEmpData()
{
Console.Write("Enter Managers Details :-");
EmpId = Convert.ToInt32(Console.ReadLine());
EName = Console.ReadLine();
Bonus = Convert.ToDouble(Console.ReadLine());
CA = Convert.ToDouble(Console.ReadLine());
}
public void DispalyEmpData()
{
Console.WriteLine("Manager Id is" + EmpId);
Console.WriteLine("Manager Name is" + EName);
Console.WriteLine("Manager Bonus is" + Bonus);
Console.WriteLine("Manager CA is" + CA);
}
static void Main(string[] args)
{
Manager Obj1 = new Manager();
Obj1.GetEmpData();
Obj1.DispalyEmpData();
Console.ReadLine();
}
}
}
output---------------------
Enter manager Detail is-- 10
ravi
15000
25000
Manager Id is:- 10
Manager Name is:- ravi
Manager Bonus is:- 15000
Manager CA is:- 25000