C# | How to Implement Multiple Interfaces Having Same Method Name
Last Updated :
04 Apr, 2019
Like a class, Interface can have methods, properties, events, and indexers as its members. But interface will contain only the declaration of the members. The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly.
C# allows the implementation of multiple interfaces with the same method name. To understand how to implement multiple interfaces with the same method name we take an example. In this example, we take two interfaces named as G1
and G2
with the same method name.
Now implement these interfaces in a class named as Geeks and define mymethod() method and when the user will try to call this method, it gives an error because we did not tell the compiler that this method belongs to which interface.
Example:
using System;
interface G1 {
void mymethod();
}
interface G2 {
void mymethod();
}
class Geeks : G1, G2 {
void mymethod()
{
Console.WriteLine( "GeeksforGeeks" );
}
}
public class GFG {
static public void Main()
{
Geeks obj = new Geeks();
obj.mymethod();
}
}
|
Compile Time Error:
prog.cs(22,7): error CS0737: `Geeks’ does not implement interface member `G1.mymethod()’ and the best implementing candidate `Geeks.mymethod()’ is not public
prog.cs(11,7): (Location of the symbol related to previous error)
prog.cs(28,7): (Location of the symbol related to previous error)
prog.cs(22,7): error CS0535: `Geeks’ does not implement interface member `G2.mymethod()’
prog.cs(17,7): (Location of the symbol related to previous error)
prog.cs(48,7): error CS0122: `Geeks.mymethod()’ is inaccessible due to its protection level
prog.cs(28,7): (Location of the symbol related to previous error)
To remove this error, we will specify the name of the interface with the method name like G1.mymethod()
. It tells the compiler that this method belongs to G1 interface. Similarly, G2.mymethod()
tells the compiler that this method belongs to the G2 interface.
Example:
using System;
interface G1 {
void mymethod();
}
interface G2 {
void mymethod();
}
class Geeks : G1, G2
{
void G1.mymethod()
{
Console.WriteLine( "GeeksforGeeks" );
}
void G2.mymethod()
{
Console.WriteLine( "GeeksforGeeks" );
}
}
public class GFG {
static public void Main () {
G1 obj = new Geeks();
obj.mymethod();
G2 ob = new Geeks();
ob.mymethod();
}
}
|
Output :
GeeksforGeeks
GeeksforGeeks
Note: You can also declare the method as public in the class that implements the interfaces. But the confusion will still remain as in the large program a user can’t differentiate which method of which interface is implemented.
Example:
using System;
interface G1 {
void mymethod();
}
interface G2 {
void mymethod();
}
class Geeks : G1, G2 {
public void mymethod()
{
Console.WriteLine( "GeeksforGeeks" );
}
}
public class GFG {
static public void Main()
{
Geeks obj = new Geeks();
obj.mymethod();
}
}
|
Output:
GeeksforGeeks
Share your thoughts in the comments
Please Login to comment...