Wednesday 19 August 2015

What is method hiding in C#.Net

                       In method Hiding also parent class methods can be redefine under child classes even if they were not declared as Virtual by using 'new' keyword.

Example :

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

namespace ConsoleApplication1
{
    class methodhiding
    {
        public void demo()
        {
            Console.WriteLine(" this is method");
        }

    }
    class methodhiding1 : methodhiding
    {
        public new void demo()
        {
            Console.WriteLine(" this is method hiding");
        }
        static void Main()
        {
            methodhiding1 m = new methodhiding1();
            methodhiding m1 = m;
            m1.demo();
            Console.WriteLine();
            m.demo();
            Console.ReadLine();
        }

    }
}


Output :
           this is method
           this is method hiding

No comments:

Post a Comment