Concept Of Polymorphism (Late Binding) In C#

Introduction

  • It is one of the primary pillars of an object-oriented programming.
  • The word polymorphism is combined using two words. “Poly” which means many and “morph” which means form.
  • An ability to take more than one form is called polymorphism.
  • An ability of different related objects to respond to the same message in different ways is called Polymorphism.
  • In other words, one command may invoke different implementation for the different related objects.

Polymorphism plays an important role in allowing the different objects to share the same external interface although the implementation may be different. Thus, it is seen that Polymorphism helps to design the extensible software components as we can add new objects to the design without rewriting the existing methods.

Explanation

As I have explained in Inheritance in previous article,

CalculateSal() function can calculate the salary for the different employees objects like Manager, Developer and SalesPerson but the ways in which all these objects would calculate the salary would be different.



Compile Time Binding and Late Binding

  • Binding is an association of function calls to an object.
  • The binding of a member function, which is called within an object and is called compiler time or static type or early binding.
  • All the methods are called an object or class name are the examples of compile time binding
  • The binding of the function calls an object at the run time is called run time or dynamic or late binding.
  • Late binding is achieved, using virtual methods, the virtual methods are overridden in the derived class as per the specific requirement.

Virtual method and override

Polymorphism provides a way for the derived class to give its own definition of a method that has already been defined by the base class. This process is called method overriding.

In method overriding, the methods have same names, same signatures, same return types but are in different scopes (classes) in the hierarchy. C# uses virtual and override keywords to implement method overriding.

When there is a possibility that a method in the base class could be overridden in the derived class, this method is marked with the virtual keyword. If the virtual method is required to have specific implementation in the derived class, it can be overridden, using override keyword.

Using method overriding, we can resolve method invocation at the runtime. It is called late binding of behavior of the object.

For example, see the screenshot given below.


Note

  1. When declaring override method, we cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.
  2. When we want to override the method inside the derived class to the base class, then we have to make the base class to virtual or abstract.
  3. The virtual modifier cannot be used with static, abstract and override modifiers.
  4. The implementation of a virtual member of the base class can be changed by an overriding member in the derived class.

Different between Method Overloading and Method Overriding


Accessibility Within AssemblyAccessibility Outside Assembly
ScopeIn the same classIn the inherited classes
PurposeHandy for the program design as different method names need not to be rememberedHandy for program design as different method names need not be remembered. But its implementation needs to be specific to the derived class
Signature of methodsDifferent for each method overloadedHas to be same in the derived class as in the base class
Types of methodsInstance and static methodsInstance methods only
Return TypeCan be same or different as it is not consideredAlways needs to be same