In this article you will learn the following topics:
- Polymorphism
- Static or Compile-time Polymorphism
- Method overloading
- Dynamic or Runtime Polymorphism
- Method Overriding
- Virtual modifier and Virtual methods
- Difference between method overriding and method hiding
- Sealed methods
What is Polymorphism?
Polymorphism means one name for many forms.
Polymorphism means one object behaving in multiple forms.
In other words, "Many forms of a single object is called Polymorphism."
One function behaves differently in multiple forms.
Real-World Example of Polymorphism
Example 1
A teacher behaves to student.
A teacher behaves to his/her seniors.
Here teacher is an object but the attitude is different in each situation.
Example 2
Person behaves son in the house at the same time that a person behaves an employee in an office.
Example 3
Your mobile phone, one name but many forms, such as:
- As phone
- As camera
- As MP3 player
- As radio
With polymorphism, the same method or property can perform different actions depending on the run-time type of the instance that invokes it.
There are two types of polymorphism:
- Static or Compile-time Polymorphism
- Dynamic or Runtime Polymorphism.

Static or Compile-time Polymorphism
- In Static Polymorphism the decision is made at compile-time.
- Which method is to be called is decided at compile-time only.
- Method overloading is an example of this.
- Compile-time polymorphism is method overloading, where the compiler knows which overloaded method it is going to call.
Method overloading is a concept where a class can have more than one method with the same name and different parameters.
The compiler checks the type and number of parameters passed to the method and decides which method to call at the compile-time and it will give an error if there are no methods that matches the method signature of the method that is called at the compile-time.
Example
- namespace MethodOverloadingByManishAgrahari
- {
- class Program
- {
- public class TestOverloading
- {
- public void Add(string a1, string a2)
- {
- Console.WriteLine("Adding Two String :" + a1 + a2);
- }
- public void Add(int a1, int a2)
- {
- Console.WriteLine("Adding Two Integer :" + a1 + a2);
- }
- }
- static void Main(string[] args)
- {
- TestOverloading obj = new TestOverloading();
- obj.Add("Manish " , "Agrahari");
- obj.Add(5, 10);
- Console.ReadLine();
- }
- }
- }

Dynamic or Runtime Polymorphism
Run-time polymorphism is done by method overriding.
Method overriding allows us to have methods in the base and derived classes with the same name and the same parameters.
By runtime polymorphism we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.
Through the reference variable of a base class, the determination of the method to be called is based on the object being referred to by a reference variable.
The compiler would not be aware of the availability of a method for overriding the functionality or not. So the compiler would not give any error at compile-time. At runtime it will be decided which method to call and if there is no method at runtime then it will give an error.
See the following example:
- namespace PolymorphismByManishAgrahari
- {
- class Program
- {
- public class Base
- {
- public virtual void Show()
- {
- Console.WriteLine("Show From Base Class.");
- }
- }
- public class Derived : Base
- {
- public override void Show()
- {
- Console.WriteLine("Show From Derived Class.");
- }
- }
- static void Main(string[] args)
- {
- Base objBase;
- objBase = new Base();
- objBase.Show();// Output ----> Show From Base Class.
- objBase = new Derived();
- objBase.Show();//Output--> Show From Derived Class.
- Console.ReadLine();
- }
- }
- }
Virtual Modifier
According to MSDN, "The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class.".
A virtual method is a method whose behavior can be overridden in a derived class.
Virtual method allows declare a method in base class that can be redefined in each derived class.
When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, that might be the original member, if no derived class has overridden the member.
By default, methods are non-virtual. You cannot override a non-virtual method.
You cannot use the virtual modifier with the static, abstract, private or override modifiers.
Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
It is an error to use the virtual modifier on a static property.
A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
Virtual method solves the following problem
In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class methods overridden by the derived class then the method call behavior is ambiguous.
In C#, polymorphism is explicit; you must have a virtual (or abstract) modifier on the base class method (member) and an override on the derived class method, that you probably already know.
If you don't put a modifier on a base class method, polymorphism can't ever happen. If you then add a non-modified method to the derived class with the same signature as the non-modified base class method then the compiler will generate a warning message.
See the following example:
- namespace PolymorphismByManishAgrahari
- {
- class Program
- {
- public class Base
- {
- public virtual void Show()
- {
- Console.WriteLine("Show From Base Class.");
- }
- }
- public class Derived : Base
- {
- //the keyword "override" change the base class method.
- public override void Show()
- {
- Console.WriteLine("Show From Derived Class.");
- }
- }
- static void Main(string[] args)
- {
- Base objBaseRefToDerived = new Derived();
- objBaseRefToDerived .Show();//Output--> Show From Derived Class.
- Console.ReadLine();
- }
- }
- }
In other languages, Java for instance, you have what is called "implicit" polymorphism where just putting the method in the derived class with the same signature as a base class method will enable polymorphism.
In Java, all methods of a class are virtual by default unless the developer decides to use the final keyword thus preventing subclasses from overriding that method. In contrast C# adopts the strategy used by C++ where the developer must use the virtual modifier for subclasses to override the method. Thus all methods in C# are non-virtual by default.
The C# approach is more explicit for the purpose of making the code safer in versioning scenarios. in other words you build your code based on a thirrd-party library and use meaningful, but common, method names. The thirrd-party library upgrades, using the same common method name. With implicit polymorphism the code would break, but with C# you would receive a compiler warning so you can double check to see if polymorphism was something you wanted to do.

SubashPosted Nov 16, 2016, 8:23 PM
Thank you share a nice article very useful one for me............
Sam HobbsPosted Jun 5, 2013, 3:10 PM
I meant C plus plus except that got messed up by the web site.
Sam HobbsPosted Jun 5, 2013, 3:08 PM
Also, it is not so clear to me what Static Polymorphism is. It might be specific to C ; see: http://stackoverflow.com/questions/4557141/static-polymorphism-definition-and-implementation
Sam HobbsPosted Jun 5, 2013, 2:48 PM
Where does the C# language specification state that "You cannot override a non-virtual method"? I have looked at ECMA-334 and I cannot find that. Note that your sample works for me when I remove the "override" modifier. The error "cannot override inherited member" you show is error Compiler Error CS0506. The documentation of that error is misleading and I have submitted relevant feedback. The sample code in that documentation is the same in the sense that the error is removed by removing the "override" modifier.