Skip to content

C# – an example to illustrate Mutiple Inheritance through interfaces

January 19, 2012

A new year and a new post. Some of the readers might find me coding in C# and Python depending on my mood. Here is an example to show how to use interfaces in your C# program. This also shows multiple inheritance that can be achieved through interfaces.

using System;

interface GetSum
{
    int GetSum();
}
interface CalcSum
{
    void CalcSum(int x,int y);
}

namespace MultiInterface
{
    class Addition : GetSum, CalcSum
    {
        int sum;
        public int GetSum()
        {
            return sum;
        }
        public void CalcSum(int x, int y)
        {
            sum = x + y;
        }
    }

    class Program
    {
        static void Main()
        {
            Addition objAdd = new Addition();

            objAdd.CalcSum(8000,200);
            Console.WriteLine("The Sum is = {0}", objAdd.GetSum());
        }
    }
}

OUTPUT:

nterface\MultiInterface\bin\Debug>MultiInterface.exe
The Sum is = 8200

About these ads
One Comment
  1. Great article on C# inheritance. Another good article I have found is C# Inheritance and Polymorphism

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

%d bloggers like this: