Polymorphism in C#

Polymorphism is one of the most important features of Object Oriented Programming that is used to allow different forms of any procedure with same name. you can use one procedure in many ways based on your requirements.
For Example we can make a procedure for calculating the area of a geomatrical figure, and can calculate the area of triangle, Area of Circle and Rectangle using the Same procedure name with different parameters for each geomatrical figure.
Polymorphism allows us to invoke methods of a derived class through base class reference during runtime.
Polymorphism provides different implementation of methods in a class that are called through the same name.
There are two types of Polymorphism available in C# they are listed below.

  • Dynamic Polymorphism OR Runtime Polymorphism OR Overriding OR Late Binding
  • Static Polymorphism OR Compiletime Polymorphism OR OverLoading Or Early Binding

Overriding in C#

Overriding is a feature that allows you to invoke derived class methods through a base class reference during runtime. Overriding is a feature that allows a derives class to provide specific implementation of a method that is already defined in a base class.
The implementations of methods in the derived class Overrides or replaces the implemetation of method in its base class.
The compiler binds a method to an object during the execution of a program(Runtime) instead of during the compilation of the program.
When you call a method , the method defined in the derived class is invoked and executed instead of one in the base class.

To invoke the method of a derived class that is already defined in the base class ,

  • We need to declare the base class method as Virtual
  • Implement the derived class method using the Override keyword

The Virtual Keyword indicates the method can be overridden in any derived class.


                    Using System
public Class BaseClass
{
public Virtual void Print()
{
console.writeline("Baseclass method");
}
}
public class Derivedclass : BaseClass
{
public Override void Print()
{
console.writeline("Child Class Print method");
}
}
public class Program
{
public static void main()
{
BaseClass B=new DerivedClass();
B.Print();
}
}

In the above program we declare Print() method into the base class and here we used Virtual keyword in the method defination because we want to override this Print() Method any where into the Base class. so we use Virtual keyword that indicates that Print() method will oveeride in any of the derived class and replaces the code for base class.

Difference between Method overriding and Method hiding .

In the Method overriding a Base Class reference variable pointing to a child class object , will invoke the overridden method in the child class.

In the Method hidding a base class refernece variable pointing to a child class object will invoke the hidden method in the base class.


                Using System
public Class BaseClass
{
public Virtual void Print()
{
console.writeline("Baseclass method");
}
}
public class Derivedclass : BaseClass
{
public Override void Print()
{
console.writeline("Child Class Print method");
}
}
public class Program
{
public static void main()
{
BaseClass B=new DerivedClass();
B.Print();
}
}

What is Overloading in C# ?

When a Compiler compiles a program , it knows the information about the method argument according to that arguments, it binds the appropriate method to an object at the compile time , this process is known as Compile time Polymorphism or Early binding.
We can implement Compile time ploymorphism through overloaded methods and operators. Overloaded methods are the methods that have same name but different Signatures, such as Number of Parameters, type of Parameters and sequence of Parameters.

There are three types of compile type polymorphism which are listed below:-

  • Method Overloading
  • OPeartor Overloading
  • Indexer Overloading

Method Overloading :- Method Overloading is a concept in which we can define many methods with same name but different Signatures, like number of parameters , type of parameters and order of Parameters passed to it.
In method overloading method behaves according to the number and type of parameters passed to it.
When you call overloaded methods a compiler automatically determines which method should be used according to the signature specified in the method call.
The Signature of method does not include the return type and the params modifiers. so, it is not possible to overload a function just based on the return type or param modifier.
Method overloading allows a class to have multiple methods with same name but with a different signture. SO in C# functions can be overloaded based on the number , type and kind of parameters.

Program for Method Overloading


                Public Class Geomatry
{
Public int Area(int _width,int _height)
{
return _width*_height;
}
Public float Area(float _width,int _height)
{
return _width*_height;
}
Public int Area(int _width)
{
return _width*_width*3.14;
}

public class Program
{
public static void main()
{
Geomatry B=new Geomatry();
B.Area(5,10);
B.Area(5.65,125);
B.Area(10);
}
}

In the above example we use three methods with same name Area() but all this three methods have different signature.
First Area method takes two integer parameter and return a integer value.
Second Area method takes one integer parameter and one float parameter and return a float value.
Third Area method takes one integer parameter and return a integer value.
When we create a object of Geomatry class , compiler automatically binds the appropriate method with the help of number of parameters passed, and typ[e of parameter passed to it.
So in the Method Overloading we use more than one method with same name but different parameters.

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.