ISourceCode

Make the frequent cases fast and the rare case correct

C# – an example to illustrate Mutiple Inheritance through interfaces

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 response to “C# – an example to illustrate Mutiple Inheritance through interfaces

  1. LINQ operations September 26, 2012 at 7:42 am

    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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: