Interviews10 answers
Is it possible to implement two interfaces in the same class if the interfaces have same method signature ,name,same number of methods etc.? If yes How?
10 Answers
Yes we can. We have to explicitly implement the interfaces in such cases. We can do that by using interface name and it's method name in the class..
Interface I1{
void m();
}
Interface I2{
void m();
}
Class A : I1, I2 {
public void I1.m(){ }
public void I2.m(){ }
}
mohammed yaseenPosted May 2, 2021, 7:00 AM EDT
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
public void Hello()
{
Console.WriteLine("Hello to all");
}
}
public class interfacetest
{
public static void Main()
{
Test Obj1 = new Test();
Obj1.Hello();
}
}
Binod KumarPosted Jun 28, 2019, 2:05 AM EDT
Yes.IT Will be possible by using explicit interface name with method name
kajal ranePosted Apr 7, 2019, 1:03 AM EDT
Yes Possible can use explicit implementation.
Ronak patelPosted Sep 13, 2018, 8:34 PM EDT
by the explicitly interface implementation
Jitendra SharmaPosted Aug 20, 2018, 11:05 AM EDT
Yes, by implementing the concept of explicit implementation of the interface where we specify InterfaceName.MethodName to avoid ambiguity.
Joy KPosted Jun 7, 2018, 2:02 PM EDT
yes, use explicit implementation of interfaces
Pravinkumar BirajdarPosted Apr 9, 2018, 9:38 PM EDT
Yes. It is called Explicit Interface Implementation.
Akbar MullaPosted Jan 8, 2018, 3:25 AM EST
yes you can implement method by name of interface first
bebo beboPosted Nov 25, 2017, 6:13 AM EST
We have use the "Explicit Interface implementation" feature of C# to achieve this. For example
// Declare the English units interface:
interface IEnglishDimensions
{
float Length();
float Width();
}
// Declare the metric units interface:
interface IMetricDimensions
{
float Length();
float Width();
}
//You can implement in the class like below
class Box : IEnglishDimensions, IMetricDimensions
{
float lengthInches;
float widthInches;
public Box(float length, float width)
{
lengthInches = length;
widthInches = width;
}
// Explicitly implement the members of IEnglishDimensions:
float IEnglishDimensions.Length()
{
return lengthInches;
}
float IEnglishDimensions.Width()
{
return widthInches;
}
// Explicitly implement the members of IMetricDimensions:
float IMetricDimensions.Length()
{
return lengthInches * 2.54f;
}
float IMetricDimensions.Width()
{
return widthInches * 2.54f;
}
}
Warning : An interface member that is explicitly implemented cannot be accessed from a class instance: You have to cast it to interface type to call the method.
Check this MSDN link for the full example :
https://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx
Niladri Sekhar DuttaPosted Jun 5, 2017, 10:43 AM EDT


