Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Multiple Inheritance With Interfaces

0.00/5 (No votes)
13 Feb 2008 1  
This articles talks about the clash situation where a function with the same name resides in two interfaces and one Derived class call it.

Introduction

This article gives an idea about the situation called interface clash. This situation occurs when two interfaces have functions with the same name and signature and one base class implements these interfaces.

Background

In earlier languages like C++, there was the concept of Multiple Inheritance. But in C#, we do not have any such feature. This was something in the list of features which is available in C++ but not in C#. Due to this feature in C++, developers were facing the problem of Diamond Inheritance. That’s why Virtual Functions came into the picture.

Using the Code

In C#, when two interfaces have functions with the same name and a class implements these interfaces, then we have to specifically handle this situation. We have to tell the compiler which class function we want to implement. For such cases, we have to use the name of the interface during function implementation. Have a look at the following example:

Blocks of code should be set as style Formatted like this:

Ex:
    /// 
    /// Interface 1
    /// 
    public interface Interface1
    {
        /// 
        /// Function with the same name as Interface 2
        /// 
        void MyInterfaceFunction();
    }

    /// 
    /// Interface 2
    /// 
    public interface Interface2
    {
        /// 
        /// Function with the same name as Interface 1
        /// 
        void MyInterfaceFunction();
    }

    /// 
    /// MyTestBaseClass Implements the two interfaces Interface1 and Interface2
    /// 
    public class MyTestBaseClass:Interface1,Interface2
    {
        #region Interface1 Members

        void Interface1.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface1 Function()");
            return;
        }

        #endregion

        #region Interface2 Members

        void Interface2.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface2 Function()");
            return;
        }

        #endregion
    }

In the above example, we are implementing the function MyInterfaceFunction() by using its interface name. In this case if we create the object of MyTestBaseClass and check for MyInterfaceFunction(), it won't be directly available. Look at the following code:

MyTestBaseClass obj = new MyTestBaseClass();
//Following code would give an error saying that
//class does not have a definition for MyInterfaceFunction.
obj.MyInterfaceFunction();

We can call the respective interface function by typecasting it to the corresponding interfaces. See the example below:

//This code would work fine and calls the function of the first interface
((Interface1)obj).MyInterfaceFunction();

//This code would also work fine and calls the function of the second interface
((Interface2)obj).MyInterfaceFunction();

Now comes the twist. We will add one more function with the same name in MyTestBaseClass. Now try to access the elements of the object of MyTestBaseClass. You will see that MyInterfaceFunction is available there. See the following code now and this code would call the function of interfaces first followed by the Class function.

Ex.
    /// 
    /// MyTestBaseClass implements the two interfaces Interface1 and Interface2
    /// 
    public class MyTestBaseClass:Interface1,Interface2
    {
        public void MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyTestBaseClass's Function()");
            return;
        }

        #region Interface1 Members

        void Interface1.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface1 Function()");
            return;
        }

        #endregion

        #region Interface2 Members

        void Interface2.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface2 Function()");
            return;
        }

        #endregion
    }

    /// 
    /// Interface 1
    /// 
    public interface Interface1
    {
        /// 
        /// Function with Same Name as Interface 2
        /// 
        void MyInterfaceFunction();
    }

    /// 
    /// Interface 2
    /// 
    public interface Interface2
    {
        /// 
        /// Function with Same Name as Interface 1
        /// 
        void MyInterfaceFunction();
    }

In the Main Function – 

MyTestBaseClass obj = new MyTestBaseClass();

//This code would work fine and calls the function of the first interface
((Interface1)obj).MyInterfaceFunction();

//This code would work fine and calls the function of the second interface
((Interface2)obj).MyInterfaceFunction();

//This code would call the class function
obj.MyInterfaceFunction();

Now comes another twist. You derive a class MyDerivedClass() from MyTestBaseClass(). Before that, remove the function last implemented in MyTestBaseClass(). Now create the instance of MyDerivedClass() and check. No function would be available with this class even though its base class is having these functions.

Ex:
    /// 
    /// Interface 1
    /// 
    public interface Interface1
    {
        /// 
        /// Function with the same name as Interface 2
        /// 
        void MyInterfaceFunction();
    }

    /// 
    /// Interface 2
    /// 
    public interface Interface2
    {
        /// 
        /// Function with the same name as Interface 1
        /// 
        void MyInterfaceFunction();
    }

    /// 
    /// MyTestBaseClass implements the two interfaces Interface1 and Interface2
    /// 
    public class MyTestBaseClass:Interface1,Interface2
    {

        //public void MyInterfaceFunction()
        //{
        //    MessageBox.Show("Frm MyTestBaseClass's Function()");
        //    return;
        //}

        #region Interface1 Members

        void Interface1.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface1 Function()");
            return;
        }

        #endregion

        #region Interface2 Members

        void Interface2.MyInterfaceFunction()
        {
            MessageBox.Show("Frm MyInterface2 Function()");
            return;
        }

        #endregion
    }

    /// 
    /// New Derived class which is derived from MyTestBaseClass
    /// 
    public class MyDerivedClass : MyTestBaseClass
    {
        //No Functions Here....
    }

So again here for getting the function of interfaces, we have to separately typecast the object of the derived class with the interface. See the code below:

//In the Main Function……

//This code would call the Interface1 function
MyDerivedClass derivedObj = new MyDerivedClass();
((Interface1)derivedObj).MyInterfaceFunction();        

Conclusion

From the above article, you should get an idea about calling interface functions when there is a clash. Your suggestions and views will be appreciated.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here