Function Overriding in C#

Function Overriding in C# with Examples

In this article, I am going to discuss Function Overriding in C# with examples. The word Function Overloading and Method overloading are interchangeably used. Please read our previous article before proceeding to this article where we discussed Function Overloading with some examples. Here in this article, we are going to discuss the following pointers with examples.

  1. What is Function Overriding in C#?
  2. When do we need to override a method in C#?
  3. When a subclass method is treated as an overridden method in C#?
  4. How a method is overridden in C#?
  5. How to execute the superclass method if it is overridden in the subclass?
  6. What are the differences between function overloading and function overriding in C#?
What function overriding in C#?

The process of reimplementing the superclass non-static method in the subclass with the same prototype (same signature defined in the superclass) is called as function overriding. The implementation of the sub-class overrides (i.e. replaces) the implementation of superclass methods.

The point that you need to keep in mind is the overriding method are always going to be executed from the current class object. Superclass method is called the overridden method and sub-class method is called as the overriding method.

When do we need to override a method in C#?

If the superclass method logic is not fulfilling the sub-class business requirements, then the subclass needs to override that method with required business logic. Usually, in most of the real-time applications, the superclass methods are implemented with generic logic which is common for all the next level sub-classes.

When is a sub-class method treated as an overriding method?

If a method in sub-class contains the same signature as the superclass non-private non-static method, then the sub-class method is treated as the overriding method and the superclass method is treated as the overridden method.

How can we override a parent class method under child class?

If you want to override a parent class method in its child class, first the method in the parent class must be declared as virtual by the using the keyword virtual, then only the child classes get the permission for overriding that method.

Declaring the method as virtual is marking the method is overridable. If the child class wants to override the parent class virtual method then the child class can do it with the help of the override modifier. But overriding the method under child class is not mandatory for the child classes.

Syntax:

Class1:

Public virtual void show(){} //virtual function (overridable)

Class2: Class1

Public override void show(){} //overriding

Let us see an example for understanding method overriding in C#.
namespace PolymorphismDemo
{
    class Class1
    {
        public virtual void show()
        {
            Console.WriteLine("Super class show method");
        }
    }
    class Class2 : Class1
    {
        public override void show()
        {
            Console.WriteLine("Sub class override show method");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class2 obj = new Class2();
            obj.show();
            Console.ReadKey();
        }
    }
}

OUTPUT:

Function Overriding in C#

How can we execute the superclass method if it is overridden in sub-class?

Once we reimplement the parent class methods under the child class, then the object of the child class calls its own methods but not its parent class method. But if you want to still consume or call the parent class’s methods from the child class, then it can be done in two different ways.

By creating parent class object under the child class, we can call the parent class methods from child class or by using the base keyword, we can call parent class methods from child class, but this and base keyword cannot be used under the static block.

Using the base keyword to call the parent class methods:

Let us see an example for better understanding

namespace PolymorphismDemo
{
    class Class1
    {
        public virtual void show()
        {
            Console.WriteLine("Super class show method");
        }
    }
    class Class2 : Class1
    {
        public override void show()
        {
            base.show();
            Console.WriteLine("Sub class override show method");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class2 obj = new Class2();
            obj.show();
            Console.ReadKey();
        }
    }
}

OUTPUT:

Function Overriding in C#

What is the difference between function overloading and function overriding in C#?
FUNCTION OVERLOADING FUNCTION OVERRIDING
It is an approach of defining multiple methods with the same name but with a different signature. It is an approach of defining multiple methods with the same name and with the same signature.
Overloading a method can be performed within a class or within the child classes also. Overriding of methods is not possible within the same class it must be performed under the child classes.
To overload a parent class method under the child class, the child class does not require permission from the parent. To override a parent class method under the child class, first, the child class require explicit permission from its parent.
This is all about defining multiple behaviors to a method. This is all about changing the behavior of a method.
Used to implement static polymorphism. Used to implement dynamic polymorphism.
This is code refinement technique. This is code replacement technique.
No separate keywords are used to implement function overloading. Use virtual keyword for base class function and override keyword in derived class function to implement function overriding.

In the next article, I am going to discuss Method Hiding in C# with examples.

SUMMARY:

In this article, I try to explain Function Overriding in C#. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *