Solution of problem having same method name in two interfaces inheriting a class

SheoNarayan
Posted by in C# category on for Advance level | Views : 28834 red flag
Rating: 5 out of 5  
 1 vote(s)

This articles describes the solution of having the same method name in two different interfaces inheriting a class.
Introduction

I think this question is very favourite to the interviewer these days. I have got many questions from visitors asking the solution of implementing the method and calling it if a class inherits two different interfaces and both interfaces have same method name and signature. This articles describes various scenario related with that and gives the solution.

Defining interfaces

To descibe this scenario, I have created a class called ExampleClass that is inheriting two interface IExampleClass1 and IExampleClass2. Both interfaces have same method name and Signature CurrentDateTime(). See the code snippet for them below.

 

First Interface: IExampleClass1

public interface IExampleClass1

{

string CurrentDateTime();

}

Second Interface: IExampleClass2

using System;

public interface IExampleClass2

{

string CurrentDateTime();

string CurrentDateTime(DateTime thisDateTime);

}

Implementing Methods

If we inherit these two interfaces into the same class then we will have to implement all the methods defined in these interfaces. Lets see the ways of implementing interface into our class. There are two ways we can implement interfaces into a class. Even when you inherit the interface and right click the interface name in the VWD or Visual Studio, you will get two options to implement interfaces.

public class ExampleClass : IExampleClass1, IExampleClass2

1. Implement Interface - When we choose this option of implementing interface, we need to define methods name that interface has and need to implement those methods. By default these methods modifiers will be public. This is also called Implicitely implementation of the interface.

#region IExampleClass2 Members

public string CurrentDateTime()

{

return "Interface 2 : " + DateTime.Now.ToString();

}

 

public string CurrentDateTime(DateTime thisDateTime)

{

return "Interface 2 : " + thisDateTime.ToLongDateString();

}

#endregion

2. Implement Interface Explicitely - When you choose this option, you need to define method names that interface has by prefixing the interface name. Notice that there is no access modifier for this method implementation so by default its scope will be internal. Internal is the access modifier for the method or class that is only accessible from the class in same assembly.

#region IExampleClass1 Members

string IExampleClass1.CurrentDateTime()

{

return "Interface 1 : " + DateTime.Now.ToString();

}

#endregion

If you try to change its access modifier to public in this scenario, you will get "CS0106: The modifier 'public' is not valid for this item" error while running your application. Because there is a public method with the same name and signature already implemented (for IExampleClass2 interface) in the class.

Using Methods

Lets see how to use methods of both interfaces implemented in the class.

ExampleClass ex = new ExampleClass();

lblMessage1.Text = ex.CurrentDateTime();

lblMessage1.Text += "<br />" + ex.CurrentDateTime(DateTime.Now);

 

IExampleClass1 ex1 = new ExampleClass();

lblMessage2.Text = ex1.CurrentDateTime();

 

IExampleClass2 ex2 = new ExampleClass();

lblMessage3.Text = ex2.CurrentDateTime();

lblMessage3.Text += "<br />Overload method: " + ex2.CurrentDateTime(DateTime.Now);

Output

In the above code snippet, you can see that I have instantiated ExampleClass in three different ways. Lets see them one by one.

1. ExampleClass ex = new ExampleClass(); - When we are instantiating the class by specifying the object as the class name, we will be able to call methods for the interface that is implicitely implemented. In this case IExampleClass2.


2. IExampleClass1 ex1 = new ExampleClass(); - If we want to call the method that is explicitely implemented, we need to specify the object as the interface name and instantiate the class then we will be able to call the method of this interface. In this case IExampleClass1.

3. IExampleClass2 ex2 = new ExampleClass(); - You can call methods of the interface that is implicitely implemented by specifying the name of the Interface as the object name (The way we have done in the 2nd point). This will give the same result as the 1st point above.

 

Now, lets play with the code and remove the implementation of the Interface 1 (IExampleClass1) and then try to run the code. You will see that you are not getting any error even if there is not implementation for the IExampleClass1 interface. This is because the implementation of the method (CurrentDateTime) for IExampleClass2 will be shared for both interfaces. In this case even if we will instantiated the ExampleClass by specifying IExampleClass as the object (point 2 above), we will see that the implementation of IExampleClass2 method is being called.


Conclusion

By explicitly defining methods name, you can implement methods of different interfaces having same name and signature in a class.

Hope this example will be useful for the readers. Thanks for reading and Happy Interfacing :) !!!

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Kak.mca on: 11/30/2008
Really Beautiful Article Narayan. Thanks.

Login to post response

Comment using Facebook(Author doesn't get notification)