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.
3 | void DisplayEmployeeDetails(); |
Above syntax shows simple demonstration of interface "IEmployee" with signature of a method "DisplayEmployeeDetails". Now let's implement the same interface to a class.
1 | class clsEmployee : IEmployee |
3 | public void DisplayEmployeeDetails(){ |
4 | Console.WriteLine(“Displaying employee details…”); |
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.
1 | public class Employee : IEmployee |
3 | void IEmployee.DisplayEmployeeDetails() |
5 | Console.WriteLine( "IEmployee Employee Name --> Questpond and Employee Code --> 009" ); |
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
1 | public interface IEmployee{ |
2 | void DisplayEmployeeDetails(); |
5 | public interface ICompany{ |
6 | void DisplayEmployeeDetails(); |
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.
1 | public class clsEmployee : IEmployee,ICompany |
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.
1 | public class Employee : IEmployee,ICompany |
3 | void ICompany.DisplayEmployeeDetails(){ |
4 | Console.WriteLine( "ICompany Employee Name --> Questpond and Employee Code --> 009" ); |
6 | void IEmployee.DisplayEmployeeDetails(){ |
7 | Console.WriteLine( "IEmployee Employee Name --> Questpond and Employee Code --> 009" ); |
Console Application
Finally let's create the objects of two interfaces "IEmployee" and "ICompany" in our main method of a Console Application program.
03 | static void Main( string [] args) |
05 | IEmployee IEmp = new clsEmployee(); |
06 | IEmp.DisplayEmployeeDetails(); |
08 | ICompany IComp = new clsEmployee(); |
09 | IComp.DisplayEmployeeDetails(); |
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.
1 | public class Employee : IEmployee |
3 | public void DisplayEmployeeDetails() |
5 | Console.WriteLine( "Employee Name --> Questpond and Employee Code --> 009" ); |
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
3 | void DisplayEmployeeDetails(); |
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.
1 | public class Employee : IEmployee |
3 | public void DisplayEmployeeDetails() |
5 | Console.WriteLine( "Employee Name --> Questpond and Employee Code --> 009" ); |
Console Application
Finally let's create the objects of an interface "IEmployee" in our main method of a Console Application program.
3 | static void Main( string [] args) |
5 | IEmployee IEmp = new clsEmployee(); |
6 | IEmp.DisplayEmployeeDetails(); |
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