Definition - What does Polymorphism mean?

Polymorphism, in C#, is the ability of objects of different types to provide a unique interface for different implementations of methods. It is usually used in the context of late binding, where the behavior of an object to respond to a call to its method members is determined based on object type at run time. Polymorphism enables redefining methods in derived classes.

Polymorphism forms one of the fundamental concepts of object-oriented programming, along with encapsulation and inheritance.

Techopedia explains Polymorphism

Method overloading, constructor overloading and operator overloading are considered compile-time (also called static or ad-hoc) polymorphism, or early binding. Method overriding, which involves inheritance and virtual functions, is called runtime (also called dynamic, inclusion, or subtyping) polymorphism, or late binding. In the case of compile-time polymorphism, identification of the overloaded method to be executed is carried out at compile time. However, in runtime polymorphism, the type of the object from which the overridden method will be called is identified at run time.

In C#, polymorphism is implemented through inheritance and the use of the keyword "virtual". Derived classes inherit the base class members, except constructors, based on their accessibility levels. Hence, the compiler generates the code to check and identify the correct object type (that is pointed to by the reference type) at runtime and the appropriate method to be called.

An example of polymorphism is an employee base class, which includes all the basic details about employees. Classes such as clerk and manager could inherit from the employee base class with specific implementations (by overriding virtual methods) wherever necessary in the derived classes.

This definition was written in the context of C#.

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
Techopedia on Twitter

Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join 150,000+ IT pros on our weekly newsletter

3 Amazing Management Tools -- All Free
3 Amazing Management Tools -- All Free:
Attention network admins, get the top tools from Solarwinds. No expiration, 100% free.
Free 30 Day Trial – VMTurbo Operations Manager
Free 30 Day Trial – VMTurbo Operations Manager:
Handles the scale and complexity of today’s virtual and cloud environments for you, keeping your application owners happy while maximizing...