In this article, we will see a situation that occurs when two interfaces have methods with the same name and the same parameter(s) and one base class implements these interfaces. For example, we create interfaces that have methods with the same name. After that we create a class that inherits from multiple interfaces.
What is Interface?
Interfaces in C# are provided as a replacement of multiple inheritance. Interface contains all abstract methods inherited by classes and structs, which must provide an implementation for each interface member declared. The keyword interface creates an interface. They are public by default.
Creating Interface with the Same Method Name
Now we create interfaces named A and B. The A and B interfaces contain functions with the same name, Hello().
interface A
{
void Hello();
}
interface B
{
void Hello();
}Now we will see how to implement these methods in my class.
using System;
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();
}
}Now Press F5 to execute the preceding program.

The preceding output creates more confusion about which method of which interface is implemented. All the methods defined in an interface should be implemented.
We need to tell the compiler which class function we want to implement. For such cases, we need to use the name of the interface during the implementation of a function. Have a look at the following example:
using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
void A.Hello()
{
Console.WriteLine("Hello to all-A");
}
}
public class interfacetest
{
public static void Main()
{
A Obj1 = new Test();
Obj1.Hello();
}
}In the preceding example to tell compiler which method of which interface is implemented in class "Test" you can use the name of the interface during the function's implementation. Now press F5 to execute the preceding program. It will show an error.

Now we implement the method in the Test class for interface B.
using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
void A.Hello()
{
Console.WriteLine("Hello to all-A");
}
void B.Hello()
{
Console.WriteLine("Hello to all-B");
}
}
public class interfacetest
{
public static void Main()
{
A Obj1 = new Test();
Obj1.Hello();
B Obj2 = new Test();
Obj2.Hello();
}
}Note: So in your example, you are calling the "Hello" method on the "Test" class. However, Test implements both Hello functions. Therefore, it can be treated as both for one object. So you need to assign an object of that class to a variable defined as that specific interface.
Now press F5 to execute it.



Sandeep KumarPosted Nov 28, 2022, 7:10 AM EST
Good Explanation !
Dharmendra Kumar PanditPosted Sep 22, 2022, 8:09 PM EDT
Nice experience and explained...
Mahesh PattarPosted Aug 14, 2021, 2:19 PM EDT
Very good article but I've a question. From the above example, If I want to call Hello method along Hello method from A & B from the same class. Is it possible? If Yes, How?
Sridhar YelagandhulaPosted Apr 25, 2020, 7:22 PM EDT
Only one interface is there in this interface two same methods are there then how to call
Indrajeet PrajapatiPosted Feb 28, 2020, 11:33 PM EST
Suppose I have created method M3() {} in Test Class, how to call that method in this scenario
Vishmay shahPosted Aug 6, 2019, 6:06 AM EDT
What will happen if I write like Test Obj3 = new Test(); Obj3.Hello();
maheswarPosted Apr 18, 2018, 6:48 AM EDT
Thanks for the article it is very clear to understand, In the last example how we call two hello methods with test class object
Bhavesh JadavPosted Mar 16, 2018, 7:38 PM EDT
Well explained with clarity.
Sadik AzamiPosted Jan 25, 2018, 9:48 AM EST
Thank for clear the confusion
Dinesh Kumar SharmaPosted Jul 27, 2017, 7:31 PM EDT
Thank for Nice description
Krushna ChopadePosted May 19, 2017, 12:43 AM EDT
Excellent descriptions, thanks a lot ...
Dhiraj DubeyPosted Dec 5, 2016, 12:36 AM EST
What is real time use of this?
Bahubali GanePosted Sep 9, 2016, 10:13 PM EDT
Sir is it possible to implement more than two interface for a single class???
Jitendra kumarPosted Dec 21, 2015, 12:49 AM EST
nice explanation...... thnks
Vinod KumarPosted Dec 19, 2015, 2:15 AM EST
thank you!!!!
Rajeesh MenothPosted Jul 28, 2015, 2:41 AM EDT
It will help for every one,good article
Deb Kumar GhosalPosted Mar 4, 2015, 7:51 PM EST
Excellent descriptions, like it.
binil josePosted Jan 12, 2015, 12:15 AM EST
op'[op[[
Aejaaz AhmedPosted May 26, 2014, 1:51 AM EDT
good article
Vinod PasiPosted Jan 2, 2014, 1:12 PM EST
Thank you for nice article...