/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu




Thursday, May 15, 2014

Implementing Interface in C# with an example

Implementing Interface in C# with an example

What is an Interface?

An interface looks like a class but has got no implementation. In an interface we cannot do any implementation but we can declare signatures of properties, methods, delegates and events.
Implementation part is been handle by the class that implements the interface. Implemented interface enforces the class like a standard contract to provide all implementation of interface members.
We can implement single interface or multiple interfaces to the class. By default interfaces are public.
We declare interface by using "interface" keyword.
1interface IEmployee{
2
3void DisplayEmployeeDetails();
4
5}
Above syntax shows simple demonstration of interface "IEmployee" with signature of a method "DisplayEmployeeDetails". Now let's implement the same interface to a class.
1class clsEmployee : IEmployee
2{
3          public void DisplayEmployeeDetails(){
4     Console.WriteLine(“Displaying employee details…”);
5           }
6
7}
As you see in our above snippet of code that implementation of an interface method signature is done by the class which implemented that interface.

Benefits of using an interface

Implemented interface enforces the class like a standard contract to provide all implementation of interface members.
In architecting the project we can define the proper naming conventions of methods, properties in an interface so that while implementing in a class we use the name conventions.
We can use the "interface" as a pointer in the different layers of applications.
We can use an interface for implementing run time polymorphism.

Various Forms of implementing interface

There are two of ways of implementing interfaces

Explicit
Implicit

Explicit interface implementation


To implement an interface explicitly we have to define an interface name followed by (".") dot operator then the method name.

Sample as shown in below snipped code.
1public class Employee : IEmployee
2{
3        void IEmployee.DisplayEmployeeDetails()
4        {
5            Console.WriteLine("IEmployee Employee Name --> Questpond and Employee Code --> 009");
6        }
7}
When you put a dot operator after an interface name you will see all the signatures defined in an interface.
In which scenario we use explicit interface implementation
When we have two different interfaces with same method name then in that scenario we can use explicit interface implementation.
Example of Explicit Interface
1public interface IEmployee{
2    void DisplayEmployeeDetails();
3}
4
5public interface ICompany{
6    void DisplayEmployeeDetails();
7}
In the above code we have two different interfaces "IEmployee" and "ICompany" with same method name. Now let's implement these two interfaces to a class "clsEmployee" as shown in below snippet of code.
1public class clsEmployee : IEmployee,ICompany
2{
3
4//class code goes here
5
6}
In order to implement an interface explicitly we have to define the interface name followed by (".") dot operator before method name as shown in below snipped code.
1public class Employee : IEmployee,ICompany
2{
3    void ICompany.DisplayEmployeeDetails(){
4            Console.WriteLine("ICompany Employee Name --> Questpond and Employee Code --> 009");
5    }
6    void IEmployee.DisplayEmployeeDetails(){
7            Console.WriteLine("IEmployee Employee Name --> Questpond and Employee Code --> 009");
8    }
9}
Console Application

Finally let's create the objects of two interfaces "IEmployee" and "ICompany" in our main method of a Console Application program.
01class Program{
02
03  static void Main(string[] args)
04 {
05    IEmployee IEmp = new clsEmployee();
06    IEmp.DisplayEmployeeDetails();
07
08    ICompany IComp = new clsEmployee();
09    IComp.DisplayEmployeeDetails();
10 }
11
12}
Let's run the application (Ctrl + F5) and display the output as shown it below.

Implicit interface implementation

To implement an interface implicitly we have to implement the interface name before the class using colon ":" operator as shown in below snipped code.
1public class Employee : IEmployee
2{
3        public void DisplayEmployeeDetails()
4        {
5            Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009");
6        }
7}
When you put a colon after a class name you will see the custom defined and as well as system defined interfaces in the program.

Example of Implicit Interface

1interface IEmployee
2{
3    void DisplayEmployeeDetails();
4}
Above code shows simple demonstration of an interface "IEmployee" with signature of a method "DisplayEmployeeDetails".

Now let's create a class "Employee" and implement the interface implicitly as shown in below snippet of code.
1public class Employee : IEmployee
2{
3        public void DisplayEmployeeDetails()
4        {
5            Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009");
6        }
7}
Console Application

Finally let's create the objects of an interface "IEmployee" in our main method of a Console Application program.
1public class Program{
2
3 static void Main(string[] args)
4 {
5    IEmployee IEmp = new clsEmployee();
6    IEmp.DisplayEmployeeDetails();
7 }
8
9}
Let's run the application (Ctrl + F5) and display the output as shown it below.
Thank you. This article is taken from http://www.onlinebuff.com/article_implementing-interface-in-c-with-an-example_18.html

No comments:

Post a Comment

Recent Posts

My Blog List