In c#, Polymorphism means providing an ability to take more than one form, and it’s one of the main pillar concepts of object-oriented programming after encapsulation and inheritance.
Generally, polymorphism is a combination of two words, poly, and another one is morphs. Here poly means “multiple” and morphs means “forms” so polymorphism means many forms.
In c#, polymorphism provides an ability for the classes to implement different methods called through the same name. It also provides an ability to invoke a derived class's methods through base class reference during runtime based on our requirements.
In c#, we have two different kinds of polymorphisms available, which are
In c#, Compile Time Polymorphism means defining multiple methods with the same name but with different parameters. Using compile-time polymorphism, we can perform different tasks with the same method name by passing different parameters.
In c#, the compile-time polymorphism can be achieved by using method overloading, and it is also called early binding or static binding.
Following is the code snippet of implementing a method overloading to achieve compile-time polymorphism in c#.
If you observe the above “Calculate” class, we defined two methods with the same name (AddNumbers), but with different input parameters to achieve method overloading, this is called a compile time polymorphism in c#.
Following is the example of implementing a compile-time polymorphism in the c# programming language.
When we execute the above c# program, we will get the result below.
This is how you can achieve compile-time polymorphism in c# by implementing a method overloading based on our requirements.
In c#, Run Time Polymorphism means overriding a base class method in the derived class by creating a similar function. This can be achieved by using override & virtual keywords and the inheritance principle.
Using run-time polymorphism, we can override a base class method in the derived class by creating a method with the same name and parameters to perform a different task.
In c#, the run time polymorphism can be achieved by using method overriding, which is also called late binding or dynamic binding.
Following is the code snippet of implementing a method overriding to achieve run time polymorphism in c#.
If you observe the above code snippet, we created two classes (“Users”, “Details”), and the derived class (Details) is inheriting the properties from the base class (Users). We are overriding the base class method GetInfo in the derived class by creating the same function to achieve method overriding; this is called a run time polymorphism in c#.
Here, we defined a GetInfo method with a virtual keyword in the base class to allow the derived class to override that method using the override keyword.
Only the methods with a virtual keyword in the base class are allowed to override in the derived class using the override keyword. To know more about it, check this Method Overriding in C#.
Following is the example of implementing a run time polymorphism in the c# programming language.
When you execute the above c# program, you will get the result below.
This is how you can achieve run time polymorphism in c# by implementing a method overriding based on our requirements.