Article Overview
- Background
- Prerequisites
- Why is Explicit Interface Implementation required?
- How to do Explicit Interface Implementation?
- How to do Default & Explicit Implementation?
- Summary
Background
A class inherits from 2 interfaces and both the interfaces have the same method name. Using explicit interface implementation the class should implement the method for both the interfaces.
There are various situations where you need to provide the same method name to multiple interfaces. Therefore at the time of implementation/inheritance you need to specify explicitly interface implementation with method names. This is known as Explicit Interface Implementation.
Let us go with some practical examples to know more about the Explicit Interface Implementation as follows:
- Why is Explicit Interface Implementation required?
- How to do Explicit Interface Implementation?
- How to do Default & Explicit Implementation?
Prerequisites
You should have a basic knowledge of Interface Implementation in C#.
Why is Explicit Interface Implementation required?
To solve the following kind of problem Explicit Interface Implementation technique is used.
- using System;
-
- namespace ConsoleApplication1
- {
- interface ICircle
- {
- void Area();
- }
-
- interface IRectangle
- {
- void Area();
- }
-
- public class Shape : ICircle, IRectangle
- {
- public void Area()
- {
- Console.WriteLine("Circle/Rectangle Area");
- }
- }
-
- class Program
- {
- static void Main(stri[] args)
- {
- Shape shape = new Shape();
-
- ((ICircle)shape).Area();
- ((IRectangle)shape).Area();
-
- Console.ReadLine();
- }
- }
- }
Output
Circle/Rectangle Area
Circle/Rectangle Area
Here, you can see that both times the same method is called.
Therefore, to implement both methods separately for interface Circle and Rectangle you have to do it with Explicit Interface Implementation as follows.
How to do Explicit Interface Implementation?
- using System;
-
- namespace ConsoleApplication1
- {
- interface ICircle
- {
- void Area();
- }
-
- interface IRectangle
- {
- void Area();
- }
-
- public class Shape : ICircle, IRectangle
- {
- void ICircle.Area()
- {
- Console.WriteLine("Circle Area");
- }
-
- void IRectangle.Area()
- {
- Console.WriteLine("Rectangle Area");
- }
-
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Shape shape = new Shape();
-
- ((ICircle)shape).Area();
- ((IRectangle)shape).Area();
-
- Console.ReadLine();
-
-
-
-
-
-
-
-
-
- }
- }
- }
Output
Circle Area
Rectangle Area
Note
- When a class explicitly implements an interface member the interface member can no longer be accessed thru class reference variable but only through the interface reference variable.
- Access modifiers are not allowed on explicitly implemented interface members.
How to do Default & Explicit Implementation?
If you want to make one of the interface methods as default then implement that method as normal and the other interface method as explicit.
This makes the default method to be accessible thru class instance.
- using System;
-
- namespace ConsoleApplication1
- {
- interface ICircle
- {
- void Area();
- }
-
- interface IRectangle
- {
- void Area();
- }
-
- public class Shape : ICircle, IRectangle
- {
- public void Area()
- {
- Console.WriteLine("Circle Area");
- }
-
- void IRectangle.Area()
- {
- Console.WriteLine("Rectangle Area");
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Shape shape = new Shape();
-
- shape.Area();
-
- ((IRectangle)shape).Area();
-
- Console.ReadLine();
- }
- }
- }
Output
Circle Area // this is default implementation
Rectangle Area // this is explicit implementation
Summary
Now, I believe you know more about why explicit interface implementation is required and how to implement it.