C# - Polymorphism

Polymorphism

Polymorphism

Polymorphism means one name many forms. Polymorphism means one object behaving as multiple forms. One function behaves in different forms. In other words, "Many forms of a single object is called Polymorphism."

Polymorphism is often expressed as 'one interface, multiple functions'.

Polymorphism provides following features:

  • It allows you to invoke methods of derived class through base class reference during runtime.
  • It has the ability for classes to provide different implementations of methods that are called through the same name.

Real World Example of Polymorphism

Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.

Polymorphism is of two types:

  • Compile time Polymorphism or Static Polymorphism
  • Runtime Polymorphism or Dynamic Polymorphism

Static Polymorphism

In c# Compile time Polymorphism is method and operators overloading. It is also called early binding.

In method overloading method performs the different task at the different input parameters.

Runtime Time Polymorphism

Runtime time Polymorphism is done using inheritance and virtual functions. Method overriding is called runtime Polymorphism. It is also called late binding.

When overriding a method, you change the behavior of the method for the derived class.