Wednesday, July 27, 2011

Difference between Overriding and New Keyword

Difference between Method Overriding and Method hiding

Method overriding allows a subclass to provide a specific implementation of a method that is already provided by base class. The implementation in the subclass overrides (replaces) the implementation in the base class.

The important thing to remember about overriding is that the method that is doing the overriding is related to the method in the base class.

When a virtual method is called on a reference, the actual type of the object to which the reference refers is used to determine which method implementation should be used. When a method of a base class is overridden in a derived class (subclass), the version defined in the derived class is used. This is so even should the calling application be unaware that the object is an instance of the derived class.

namespace PolymorphismByManishAgrahari
{
    class Program
    {
        public class Base
        {
     
            public virtual void Show()
            {
                Console.WriteLine("Show From Base Class.");
            }
        }

        public class Derived : Base
        {
//the keyword "override" change the base class method.
            public override void Show()
            {
                Console.WriteLine("Show From Derived Class.");
            }
        }
        static void Main(string[] args)
        {
            Base objBaseRefToDerived  = new Derived();
            objBaseRefToDerived .Show();//Output--> Show From Derived Class.
          
            Console.ReadLine();
        }
    }
}
Output--> Show From Derived Class

Method hiding does not have a relationship between the methods in the base class and derived class. The method in the derived class hides the method in the base class.

namespace PolymorphismByManishAgrahari
{
    class Program
    {
        public class Base
        {

            public virtual void Show()
            {
                Console.WriteLine("Show From Base Class.");
            }
        }

        public class Derived : Base
        {

            public new void Show()
            {
                Console.WriteLine("Show From Derived Class.");
            }
        }
        static void Main(string[] args)
        {
            Base objBaseRefToDerived  = new Derived();
            objBaseRefToDerived .Show();//Output--> Show From Base Class.
          
            Console.ReadLine();
        }
    }
}
Output is:à Show From Base Class.
In the preceding example, Derived.Show will be called; because, it overrides Base.Show.

  • The c# language specification states that  "You cannot override a non-virtual method." see following example:-
namespace PolymorphismByManishAgrahari
{
    class Program
    {
        public class Base
        {

            public void Show()
            {
                Console.WriteLine("This is Base Class.");
            }
        }

        public class Derived : Base
        {
            //Following Line will give error.
            /*
             Error:- 'PolymorphismByManishAgrahari.Program.Derived.Show()'
cannot override inherited member  'PolymorphismByManishAgrahari.Program.Base.Show()'
             * because it is not marked virtual, abstract, or override
             */
            public override void Show()
            {

                Console.WriteLine("This is Derived Class.");
            }
        }
        static void Main(string[] args)
        {
            Base objBase = new Base();
            objBase.Show();//    Output ----> This is Base Class.

            Derived objDerived = new Derived();
            objDerived.Show();//Output--> This is Derived Class.

            Base objBaseRefToDerived = new Derived();
            objBaseRefToDerived.Show();//Output--> This is Base Class.

            Console.ReadLine();
        }
    }
}
Error:- 'PolymorphismByManishAgrahari.Program.Derived.Show()' cannot override inherited member 'PolymorphismByManishAgrahari.Program.Base.Show()' because it is not marked virtual, abstract, or override

2 comments:

varsha Gupta said...

its a very useful article for those who always ready to learn more n more things .............thanks a lot

varsha Gupta said...

its a very useful article for those who always ready to learn more n more things .............thanks a lot