Introduction: Multiple Inheritance In C# Using Interfaces

In this post, we will learn about Multiple Inheritance In C# Using Interfaces with an example.

Description:

In my previous posts I have explained various topics likeĀ Optional Arguments In C#, Introduction To JSON, AJAX Basics, Console Application, ASP.NET WEBFORM, ASP.NET MVC.



Now in this post, I will explain how to achieve Multiple Inheritance In C# Using Interfaces with appropriate example.
Here I will explain simple mathematical operation program demonstrating how multiple inheritances can be achieved in C# using Interface Concept.
Now create Console Application in Visual Studio and write below lines of code in it.

using System;
using System.Collections;

namespace ConsoleDemo
{   
    class Program
    {
        interface Operation1
        {
            int AddNumber(int no1, int no2);
        }
        interface Operation2
        {
            int SubtractNumber(int no1, int no2);
        }
        interface Operation3
        {
            int MultiplyNumber(int no1, int no2);
        }
        interface Operation4
        {
            int DivideNumber(int no1, int no2);
        }

        class Calculation : Operation1, Operation2, Operation3, Operation4
        {
            public int result1;
            public int AddNumber(int a, int b)
            {
                return result1 = a + b;
            }
            public int result2;
            public int SubtractNumber(int x, int y)
            {
                return result2 = x - y;
            }
            public int result3;
            public int MultiplyNumber(int r, int s)
            {
                return result3 = r * s;
            }
            public int result4;
            public int DivideNumber(int c, int d)
            {
                return result4 = c / d;
            }

            class Program
            {
                static void Main(string[] args)
                {
                    Calculation c = new Calculation();
                    c.AddNumber(15, 30);
                    c.SubtractNumber(200, 150);
                    c.MultiplyNumber(15, 5);
                    c.DivideNumber(50, 10);
                    Console.WriteLine("Multiple Inheritance concept Using Interfaces :\n ");
                    Console.WriteLine("Addition: " + c.result1);
                    Console.WriteLine("Substraction: " + c.result2);
                    Console.WriteLine("Multiplication :" + c.result3);
                    Console.WriteLine("Division: " + c.result4);
                    Console.ReadKey();
                }
            }
        }

    }   
}

 

Multiple Inheritance In C# Using Interfaces

Multiple Inheritance In C# Using Interfaces

Summary

I hope you got an idea about Multiple Inheritance In C# Using Interfaces.
I would like to have feedback from my blog readers.
Your valuable feedback, question, or comments about this article are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website