Wednesday 19 August 2015

What is overriding method in C#.Net

Method Overriding :
                                         If a method of a class is redefine under it's child class with the same signature it's known as method overriding. This can be done only within the child class.To override a method in the child class we require an explicit from the parent class.


                                      We can override a parent class method under it's child class only when the parent class declare the method as Virtual Method of parent class can be Overridden redefine in the child class using the override modifier.
Example :  Parentclass.cs

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

namespace ConsoleApplication1
{
    class Override
    {
        public virtual void test()
        {
            Console.WriteLine("this is virtual method");
        }

    }
}

ChildClass.cs of override : 

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

namespace ConsoleApplication1
{
    class override1 : Override
    {
        public override void test()
        {
            Console.WriteLine("override method");

        }
        static void Main()
        {
            Override o;
            // first output is displyed
            o = new override1();

            // two output is displyed
            o = new Override();
            o.test();

            Console.ReadLine();
        }
    }
}

OutPut :


Note1 : 
           Overriding is the only optional for the child classes to override the virtual methods of it's parent class.

Note 2 : 
             In inheritance reference's of parent class that are created from object of a child class cannot invoke child class members.
              But we have an exemption overriding parent class references created child class object can invoke the override methods child class. Because overriding can performed only with the permission of parent class. To check this below code under main method of class override1.cs as following.

Example : Override.cs



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

namespace ConsoleApplication1
{
    class Override
    {
        public virtual void test()
        {
            Console.WriteLine("this is virtual method");
        }
        public virtual void test(int x)
        {
            Console.WriteLine("this is virtual method1");
        }

    }
}

Override1.cs :

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

namespace ConsoleApplication1
{
    class override1 : Override
    {
        public override  void test()
        {
          Console.WriteLine("override method");

        }
        public override void test(int x)
        {
            Console.WriteLine("this is override method " + x );
        }

        public void Ptest(int x)
        {
            base.test(x);
        }


        static void Main()
        {
            //override1 o = new override1();
            ////Override o1 = new Override();
            //Override o1 = o;
            //o1.test();

            //o.Ptest(10);
            //o.test();
            //o.test(10);

            override1 o = new override1();
            Override o1 = o;
            o1.test(10);
            o.test(10);
            o.Ptest(10);

            Console.ReadLine();
        }
    }
}


Output :



No comments:

Post a Comment