Click here to Skip to main content
Click here to Skip to main content

Multiple Inheritance With Interfaces

, 13 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
    /// <summary />
    /// Interface 1
    /// </summary />
    public interface Interface1
    {
        /// <summary />
        /// Function with the same name as Interface 2
        /// </summary />
        void MyInterfaceFunction();
    }

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

    /// <summary />
    /// MyTestBaseClass Implements the two interfaces Interface1 and Interface2
    /// </summary />
    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.
    /// <summary />
    /// MyTestBaseClass implements the two interfaces Interface1 and Interface2
    /// </summary />
    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
    }

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

    /// <summary />
    /// Interface 2
    /// </summary />
    public interface Interface2
    {
        /// <summary />
        /// Function with Same Name as Interface 1
        /// </summary />
        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:
    /// <summary />
    /// Interface 1
    /// </summary />
    public interface Interface1
    {
        /// <summary />
        /// Function with the same name as Interface 2
        /// </summary />
        void MyInterfaceFunction();
    }

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

    /// <summary />
    /// MyTestBaseClass implements the two interfaces Interface1 and Interface2
    /// </summary />
    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
    }

    /// <summary />
    /// New Derived class which is derived from MyTestBaseClass
    /// </summary />
    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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ANURAG VISHNOI
Technical Lead SAP Labs India Ltd., Gurgaon, Haryana, India
India India
I am a Sr. Software Developer in a well known MNC Software Company SAP Labs, located in Bangalore. Which is in National Capital Region of India. I have around 5 years of experience in C#.net technologies. I would like to read and write the articles related to Dot Net framework as well as Object Oriented Concepts.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 14 Feb 2008
Article Copyright 2008 by ANURAG VISHNOI
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid