Saturday, October 5, 2013

Multiple Polymorphisim

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    // step 1 (1)
    interface InterfaceA
    {
        int add(int a, int b);
        int Multi(int x, int y);
    }

    //Step 1(b)
    interface InterfaceB
    {
        int add(int a, int b);
        int Sub(int x, int y);
    }

    //Step 2
    class MultiInterfaces : InterfaceA, InterfaceB
    {

        int InterfaceA.add(int a, int b)
        {
            return a + b;
        }

        int InterfaceA.Multi(int x, int y)
        {
            return x * y;
        }

        int InterfaceB.add(int a, int b)
        {
            return a + b + 200;
        }

        int InterfaceB.Sub(int x, int y)
        {
            return x - y;
        }
    }

    //Step 3
    //Calling the methods from the inherited classf from step 2
    class ClsTestInterfaces : MultiInterfaces
    {
        static void Main()
        {

            //MultiInterfaces objMulIntf = new MultiInterfaces();
          
             //Runtime polymorphism
            InterfaceA ia = new MultiInterfaces();
            InterfaceB ib = new MultiInterfaces();

            Console.WriteLine("{0}",ia.add(10,20));
            Console.WriteLine("{0}", ia.Multi(10, 20));
            Console.WriteLine("{0}", ib.add(10, 20));
            Console.WriteLine("{0}", ib.Sub(10, 20));

            Console.ReadLine();

        }
    }

}

No comments: