If the class implements two interfaces that contain a member with the same signature, to implement those interface members in class we need the help of explicit interface.
- return_Type Interface_Name.Method_Name()
- {
-
- }
Note
The modifier is not valid to implement the explicit interface.
Example 1
Create two interfaces having only one method with the same signature as follows.
- interface IVendor
- {
- void GetVendor();
- }
- interface ISupplier
- {
- void GetVendor();
- }
Create one class name like 'UserService' to implement those interfaces.
- public class UserService:IVendor,ISupplier
- {
-
- void IVendor.GetVendor()
- {
- Console.WriteLine("GetVendor() method called from IVendor interface");
- }
- void ISupplier.GetVendor()
- {
- Console.WriteLine("GetVendor() method called from ISupplier interface");
- }
- }
If you try to implement the interface member without 'interface name', then it will throw an error as follows.
How to call the interface member?
In an explicit interface, we cannot call interface member directly using object of the class. We have two ways of calling explicit interfaces as following.
There are two ways of calling explicit interface member:
- The first way is to create the instance of the class and typecast the interface type.
- The second way is to create an object of the class and reference variable of the interface type.
- class Program
- {
- static void Main(string[] args)
- {
-
- UserService userService = new UserService();
- ((IVendor)userService).GetVendor();
- ((ISupplier)userService).GetVendor();
-
-
-
- IVendor vendor = new UserService();
- vendor.GetVendor();
- ISupplier supplier = new UserService();
- supplier.GetVendor();
-
- Console.ReadLine();
- }
- }
You can use either one of the ways of calling explicit interface members as per the requirement.
Example 2
- using System;
-
- namespace CSInterfaceDemo.Customer
- {
- interface IService1
- {
- void Print();
- }
- interface IService2
- {
- void Print();
- }
- public class Customer:IService1, IService2
- {
- void IService1.Print()
- {
- Console.WriteLine("IService1.Print()");
- }
- void IService2.Print()
- {
- Console.WriteLine("IService2.Print()");
- }
- }
-
- class program
- {
- static void Main(string[] args)
- {
-
- Customer customer = new Customer();
- ((IService1)customer).Print();
- ((IService2)customer).Print();
-
-
- IService1 service1 = new Customer();
- service1.Print();
- IService2 service2 = new Customer();
- service2.Print();
-
- Console.ReadLine();
- }
- }
- }
Implicit Interface
The explicit interface does not have the same signature of the interface member. We can call the implicit interface member by using the object of the class.
Note
The modifier is required to implement the implicit interface.
How to implement the implicit interface?
- modifier return_type interface_method_name()
- {
- ....
- }
The following is an example of the implicit interface.
Example 1
- using System;
-
- namespace CSInterfaceDemo.Customer
- {
- interface IService1
- {
- void Print();
- }
- interface IService2
- {
- void Display();
- }
- public class Customer:IService1, IService2
- {
- public void Print()
- {
- Console.WriteLine("IService1.Print()");
- }
- public void Display()
- {
- Console.WriteLine("IService2.Display()");
- }
- }
-
- class program
- {
- static void Main(string[] args)
- {
- Customer customer = new Customer();
- customer.Print();
- customer.Display();
-
- Console.ReadLine();
- }
- }
- }
The best practice to work with the interface is by adding new code. You can achieve a new requirement by adding new separate interface instead of changing old source code. This is the advice from the Interface Segregation Principle (ISP),
“Clients should not be forced to depend on methods that they do not use.” by Robert C. Martin.
I hope you understood the basic concept of explicit and implicit interface.