Place Ads here

Thursday, February 2, 2012

Method OverRiding


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


namespace MethodOverriding1
{
    //sealed class M
    //{
        public class Base
        {


            public virtual void Test(int a, int b)//"Virtual Keyword" is used when u need to 
                                                 //override same Method called as Test in Child Class.
            {
                int c;
                c = a + b;
                System.Console.WriteLine("Add is: " + c);
            }
        }
        public class Child : Base
        {


            public override void Test(int a, int b)
            {
                int c;
                c = a * b;
                System.Console.WriteLine("Multiplication is: " + c);
            }


            public class Child1 : Base
            {


                public override void Test(int a, int b)
                {
                    int c;
                    c = a / b;
                    System.Console.WriteLine("Division is: " + c);
                }


            }




            static public void Main()
            {
                Base B = new Base();
                Child c = new Child();
                Child1 d = new Child1();


                B.Test(10, 20);
                c.Test(30, 40);
                d.Test(20, 5);




            }
        }


    }
//}

No comments:

Post a Comment