Implement multiple interfaces with same function name and parameters in a class

Few days ago, I got the consultancy work for one of my friend’s firm. In their project i have had faced one interesting issue where there were two interfaces which contain same function name with same parameters and i needed to implement it in one class.

Rather than go into much theory let’s create this situation and see how we can get rid of this situation.

But first we see that what happen if we implement those interfaces in one class. So let’s create the required interfaces and class. So here we have our first interface named – “IStudent

public interface IStudent
{
	///<summary>
	/// Get list of student who have birthday today
	/// </summary>
	/// <returns></returns>
	DataTable GetTodaysBirthdayList(string date = "");
}

And here we have second interface.

public interface IStaff
{
	///<summary>
	/// Get list of staff member who have birthday today
	/// </summary>
	/// <returns></returns>
	DataTable GetTodaysBirthdayList(string date = "");
}

Now let’s try to implement these two interfaces in class.

public class School : IStudent, IStaff
{
	#region IStudent Members

	public DataTable GetTodaysBirthdayList(string date = "")
	{
		var dtList = new DataTable();
		// Business logic to get list of student who have birthday today
		return dtList;
	}

	#endregion

	#region IStaff Members

	public DataTable GetTodaysBirthdayList(string date = "")
	{
		var dtList = new DataTable();
		// Business logic to get list of staff who have birthday today
		return dtList;
	}

	#endregion
}

Alright let’s rebuild the application and see what happen. Whoaa… we get the error!

InterfaceError

Error: Type “School” already defines a member “GetTodaysBirthdayList”

I think no need to explain the error message. Now let’s remove any of the method from the class and rebuild the application. Error is gone now, But the problem is how can you know that which interface method you wanna call!

So to overcome to this situation we need to explicitly implement those interfaces. How ? Let’s see this practically.

public class School : IStudent, IStaff
{

	#region IStudent Members

	DataTable IStudent.GetTodaysBirthdayList(string date)
	{
		var dtList = new DataTable();
		// Business logic to get list of studnet who have birthday today
		Console.WriteLine("=> From IStudent interface.");
		return dtList;
	}

	#endregion

	#region IStaff Members

	DataTable IStaff.GetTodaysBirthdayList(string date)
	{
		var dtList = new DataTable();
		// Business logic to get list of staff who have birthday today
		Console.WriteLine("=> From IStaff interface.");
		return dtList;
	}

	#endregion

	// You cannot directly access explicitly implemented interface method
	// So we need two public method to call above interface method

	public DataTable GetTodaysBirthdayListOfStudent(string date = "")
	{
		IStudent objStudent = (IStudent)this;
		return objStudent.GetTodaysBirthdayList();
	}

	public DataTable GetTodaysBirthdayListOfStaff(string date = "")
	{
		IStaff objStaff = (IStaff)this;
		return objStaff.GetTodaysBirthdayList();
	}
}

By creating an instance of the above class you cannot directly access explicitly implemented interface method. For that we need two public method to call implemented interface method as shown in above class.

And now its time to call the above class method, like this –

static void Main(string[] args)
{
    var school = new School();
            
    var staffList = school.GetTodaysBirthdayListOfStaff();
    var studnetList = school.GetTodaysBirthdayListOfStudent();
}

And we are done. Well after seeing above code you might think that instead of following the above stuff i can make one common interface with common method and inherit that interface in IStaff and IStudent interface. Well Ofcouse you can do that, But consider a situation where you are using any third party DLL or in your existing application you cannot alter or made any changes due to any architectural restriction.

Alright, Hope you enjoyed this post. Thanks for reading this post.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s