Method Overriding in C#


Method overriding is the most powerful feature of object oriented programming. Method Overriding can be defined as changing the definition or behavior of the Methods of the same Inheritance/Hierarchy in order to override their logic. Method Overriding is used to achieve runtime polymorphism. Method Overriding is accomplished by using the three keywords.

  • Virtual

  • override

  • Base

  • new


  • Using the keywords virtual , override and Base we can extend or override the logic of the methods derived in Base class. But the keyword new keyword we can hide the method derived in the base class.

    Let's see the use of each keyword in detail.

  • Virtual
  • - This keyword is used with the method which can be overridden.
  • override
  • - This keyword is used with the method which is overriding the same method derived in the base class.
  • Base
  • - This keyword is used to call the base class method in the derived class.
  • new
  • - This keyword is used to hide the base class implementation of method.

    using System;
    namespace Method_overriding
    {
        class Override
        {
         public virtual void show()
         {
          Console.WriteLine("Baseclass Show");
         }
        }
        class override2:Override
        {
         public override void show()
         {
         base.show(); Calling base class method
         Console.WriteLine("Sub show");
         }
        }
        class Program
        {
         static void Main(string[] args)
         {
          Override over = new Override();
          over.show();
          Console.ReadLine();
         }
        }
    }


    Output
    Baseclass Show


    Explanation

  • We created a class override and a method show() with the keyword virtual.
  • Then we created another class override2 inherited from override and defined same method show() with the keyword override in order to override parent class method show().
  • In the subclass show() method, we called base class show() method using keyword base.
  • in the main method, we created Object of override class and called show() method.
  • Let's see another example

    using System;
    namespace Method_overriding
    {
        class Override
        {
         public virtual void show()
         {
          Console.WriteLine("Baseclass Show");
         }
        }
        class override2:Override
        {
         public override void show()
         {
         base.show(); Calling base class method
         Console.WriteLine("Sub show");
         }
        }
        class Program
        {
         static void Main(string[] args)
         {
          Override over = new Override2();
          over.show();
          Console.ReadLine();
         }
        }
    }


    Output
    Baseclass Show
    Sub show


    Explanation

  • We created a class override and a method show() with the keyword virtual.
  • Then we created another class override2 inherited from override and defined same method show() with the keyword override in order to override parent class method show().
  • In the subclass show() method, we called base class show() method using keyword base.
  • in the main method, we created Object of override class and gave the reference of override2 class and called show() method.


  • Now we will remove the Base.show() from the subclass show() method.

    using System;
    namespace Method_overriding
    {
        class Override
        {
         public virtual void show()
         {
          Console.WriteLine("Baseclass Show");
         }
        }
        class override2:Override
        {
         public override void show()
         {
         base.show() removed
         Console.WriteLine("Sub show");
         }
        }
        class Program
        {
         static void Main(string[] args)
         {
          Override over = new Override2();
          over.show();
          Console.ReadLine();
         }
        }
    }


    In the above program , note we have created object of parent class but given the reference of subclass.

    Output
    Sub show


    Use of new keyword

    As you see above , with the virtual, override keywords we override the parent class method with subclass method. But if you see , if we created object of parent class and gave reference of subclass , then the subclass method will be called not base class method.
    But in case of new keyword, there is slight difference. If you create object of Parent class and gave the reference of subclass , then the parent class method will be called, because the new keyword hides the implementation of base class method in derived class. see the below example.

    using System;
    namespace Method_overriding
    {
        class Override
        {
         public virtual void show()
         {
          Console.WriteLine("Baseclass Show");
         }
        }
        class override2:Override
        {
         public new void show()
         {
              Console.WriteLine("Sub show");
         }
        }
        class Program
        {
         static void Main(string[] args)
         {
          Override over = new Override2();
          over.show();
          Console.ReadLine();
         }
        }
    }


    Output
    Baseclass Show


    Please comment below if you have any questions.
    Method Overriding in C# Method Overriding in C# Reviewed by LanguageExpert on April 27, 2018 Rating: 5

    No comments