Skip to main content

#OOPS Multiple Inheritance With Interfaces

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 Classfunction.
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:
[__strong__]//In the Main Function……

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

Source : https://www.codeproject.com/Articles/23628/Multiple-Inheritance-With-Interfaces

Comments

Popular posts from this blog

Use Log4Net in C# windows form Application

we are going to learn on how to use the Log4Net library for creating logs. Create a new windows form application in VS. Install Log4Net library Add to AssemblyInfo.cs  Configure in  App.config Use in code  Install Log4Net library Then install the Log4Net library from the Nuget Package library.           log4net by The Apache software foundation 2.0.8 (i installed the latest). Add to AssemblyInfo.cs  After installing this package, open up AssemblyInfo .cs file under the Properties folder and add the log4net assembly information into it (under the other assembly information.).    [assembly: log4net.Config.XmlConfigurator(Watch= true )]  Configure in  App.config Now, open the App.config file and enter required details for LogNet to work. <configSections>       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2....

#MVC : Why does Html.Label() not work with periods? or Why is @Html.Label() removing some characters

You are misusing the  Html.Label  method. It is for: Returns an HTML label element and the  property name of the property  that is represented by the specified expression. That's why it gets confused if you have a point  .  in the first parameter because it expects a property expression there. However, you can use the second overload: @Html . Label ( "" , String . Format ( "{0}. someText" , 1 )) Or just write out the HTML: <label> @String . Format ( "{0}. someText" , 1 )</ label > or <label class="WelcomeText" style="float: left">Welcome @Html.Label("", Model.USERID + " ( " + Model.ROLE + " )", new { @class = "WelcomeText" })</label> 

#SQL CTE(Common Table expressions)

SQL Server 2005 and on wards, a very powerful feather has been added for the programmers' benefit called CTE. CTE is again a temporary result set derived from the underling definition.  Common Table Expressions offer the same functionality as a view, but are ideal for one-off usages where you don't necessarily need a view defined for the system. CTE offers the advantages of improved readability and ease in maintenance of complex queries . The query can be divided into separate, simple, logical building blocks. These simple blocks can then be used to build more complex, interim CTEs until the final result set is generated. Common Table Expression Syntax A Common Table Expression contains three core parts: The CTE name (this is what follows the WITH keyword) The column list (optional) The query (appears within parentheses after the AS keyword) The query using the CTE must be the first query appearing after the CTE. Syntax : WITH expression_name [ ( column_n...