Hi readers,
Myself Chetan Ranpariya and this is my first post online.
Myself Chetan Ranpariya and this is my first post online.
I am starting a series of posts where I will explain about all possible ways to implement interfaces with one or more methods with same signature.
While doing object oriented programming it is not very common but it’s not impossible also to have situation where we have more than interfaces in our design with one or more methods with same signature and we need to implement them in a single class.
Example:
Let's say we have two interfaces defined as following.
public interface Interface1
{
void Display();
}
public interface Interface2
{
void Display();
}
and there is a class which is implementing both the interfaces.
public class MyClass : Interface1, Interface2
{
}
I have been asking solution of this situation to interview candidates and I got very less number of times a proper and satisfactory answer.
I am putting the solution of this problem resulting from my understanding of OO principles, C# and my experience so far.
Here we have 3 possible definitions of class MyClass.
1. Explicit Implementation.
In this case MyClass would look like as following.
public class MyClass: Interface1, Interface2
{
void Interface1.Display()
{
Console.WriteLine("Interface1 implementation");
}
void Interface2.Display()
{
Console.WriteLine("Interface2 implementation");
}
}
Now we can call "Display" method on Interface1 and Interface2 as following.
Interface1 i1 = new MyClass();
i1.Display();
Interface2 i2 = new MyClass();
i2.Display();
When above code will be executed, appropriate version of "Display" method will be identified from class MyClass and will be executed.
This is the most accepted and practiced approach. But it has following cons.
i. This is suitable only when we require different implementations of method "Display" for both the interfaces.
In case we have only one implementation for both the interfaces, we will end up having duplicate code.
Any normal programmer would resolve this by creating a private method with common implementation and calling it from "Display" method.
public class MyClass : Interface1, Interface2
{
void Interface1.Display()
{
DoDisplay();
}
void Interface2.Display()
{
DoDisplay();
}
private void DoDisplay()
{
Console.WriteLine("Common implementation for both interfaces.");
}
}
ii. Method "Display" can not be called from instance from MyClass as class does not contain any visible definition of "Display" method specific to it because explicit implementation keeps the interface members out of the class.
But you can do as following.
OR
MyClass objIntance = new MyClass();
objIntance.Display(); //This will be a compile time error.
But you can do as following.
Interface1 objIntance = new MyClass();
((MyClass)objIntance).Display();
OR
Interface2 objIntance = new MyClass();
((MyClass)objIntance).Display(); Or else we can have a separate version of "Display" method specific to MyClass.
public class MyClass : Interface1, Interface2
{
void Interface1.Display()
{
Console.WriteLine("Interface1 implementation");
}
void Interface2.Display()
{
Console.WriteLine("Interface2 implementation");
}
public void Display()
{
Console.WriteLine("MyClass specific implementation");
//Explicitly implemented Display method for Interface1 or Interface2
//can not be called from here.
//can not be called from here.
}
} Now we can call "Display" method on instance of MyClass as following.
MyClass objIntance = new MyClass();
objIntance.Display(); Remember, here we can not call any explicitly implemented method from class specific version of method "Display".
2. Implicit Implementation
When we implement both the interfaces implicitly, MyClass would look like
3. Mixed mode of implementation.
As it is clearly saying in this mode we have interfaces implemented both implicitly and explicitly.
Look at the definition of MyClass as shown below.
Here again we have different definitions for "Display" method for Interface1 and Interface1. We have implemented "Display" method explicitly only for Interface1. In this case compiler would consider other implementation of "Display" method which is public applicable for Interface1. So again build is successful.
Let's call "Display" method.
This mixed implementation can be achieved other way around also where "Display" is explicitly implemented for Interface2 and a common implementation will be there for Interface1 and MyClass.
In next part we will talk about, what if we do not want a private method for common implementation and how that can be achieved with lesser number of lines of code.
public class MyClass : Interface1, Interface2
{
public void Display()
{
Console.WriteLine("Common implementation for both interfaces and MyClass");
}
}
Now when we call method "Display" on Interface1 or Interface2 or MyClass the same version of the method will be executed.
There can be an question here that whether compiler would get confused that for which interface "Display" method is implemented. The answer is NO.
Compiler first would look for any explicit implementations (prefixed with interface name) for interface methods. If it is able to find all methods explicitly implemented for all the interfaces, it's happy. Otherwise, it will look for a public method with the same signature in the implementing class and if it's available again compiler is happy, else compile error will be thrown.
In code above in absence of explicitly implemented methods the compiler is able to find public "Display" method with matching signature in MyClass. So it's building successfully.
It's obvious here that this approach is useful when we have a common implementation of "Display" method for both Interface1 and Interface2. Also we can call Display method on instance of MyClass.
There can be an question here that whether compiler would get confused that for which interface "Display" method is implemented. The answer is NO.
Compiler first would look for any explicit implementations (prefixed with interface name) for interface methods. If it is able to find all methods explicitly implemented for all the interfaces, it's happy. Otherwise, it will look for a public method with the same signature in the implementing class and if it's available again compiler is happy, else compile error will be thrown.
In code above in absence of explicitly implemented methods the compiler is able to find public "Display" method with matching signature in MyClass. So it's building successfully.
It's obvious here that this approach is useful when we have a common implementation of "Display" method for both Interface1 and Interface2. Also we can call Display method on instance of MyClass.
Interface1 i1 = new MyClass();
i1.Display();
Interface2 i2 = new MyClass();
i2.Display();
All three call to "Display" method shown above will be executing the same version of "Display".MyClass objInstance = new MyClass();
objInstance.Display();
3. Mixed mode of implementation.
As it is clearly saying in this mode we have interfaces implemented both implicitly and explicitly.
Look at the definition of MyClass as shown below.
public class MyClass : Interface1, Interface2
{
void Interface1.Display()
{
Console.WriteLine("Interface1 implementation");
}
public void Display()
{
Console.WriteLine("Common implementation for Interface2 and MyClass");
}
}
Scenarios like this are rarest of rare but I am explaining this for the sake of information.Here again we have different definitions for "Display" method for Interface1 and Interface1. We have implemented "Display" method explicitly only for Interface1. In this case compiler would consider other implementation of "Display" method which is public applicable for Interface1. So again build is successful.
Let's call "Display" method.
Interface1 i1 = new MyClass();
i1.Display(); //Explicit implementation for Interface1 will be executed.
Interface2 i2 = new MyClass();
i2.Display(); //Common implementation for Interface2 and MyClass will be executed.
MyClass i2 = new MyClass();
i2.Display(); //Common implementation for Interface2 and MyClass will be executed.
This mixed implementation can be achieved other way around also where "Display" is explicitly implemented for Interface2 and a common implementation will be there for Interface1 and MyClass.
In next part we will talk about, what if we do not want a private method for common implementation and how that can be achieved with lesser number of lines of code.
I hope you will find this post helpful and learning. Feel free to post your comments/views/suggestions, those will help me improving myself.
Thanks and regards,
Chetan Ranpariya