Polymorphism
What is Polymorphism? Explain different types of polymorphism.
Polymorphism is a Greek word, meaning "one name many forms". In other words, one object has many forms or has one name with multiple functionalities. "Poly" means many and "morph" means forms. Polymorphism provides the ability to a class to have multiple implementations with the same name. It is one of the core principles of Object Oriented Programming after encapsulation and inheritance.
Types of Polymorphism
There are two types of polymorphism in C#:
- Static / Compile Time Polymorphism.
- Dynamic / Runtime Polymorphism.
Static or Compile Time Polymorphism
It is also known as Early Binding. Method overloading is an example of Static Polymorphism. In overloading, the method / function has a same name but different signatures. It is also known as Compile Time Polymorphism because the decision of which method is to be called is made at compile time. Overloading is the concept in which method names are the same with a different set of parameters.
- public class TestData
- {
- public int Add(int a, int b, int c)
- {
- return a + b + c;
- }
- public int Add(int a, int b)
- {
- return a + b;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- TestData dataClass = new TestData();
- int add2 = dataClass.Add(45, 34, 67);
- int add1 = dataClass.Add(23, 34);
- }
- }
Dynamic / Runtime Polymorphism
Dynamic / runtime polymorphism is also known as late binding. Here, the method name and the method signature (number of parameters and parameter type must be the same and may have a different implementation). Method overriding is an example of dynamic polymorphism.
Method overriding can be done using inheritance. With method overriding it is possible for the base class and derived class to have the same method name and same something. The compiler would not be aware of the method available for overriding the functionality, so the compiler does not throw an error at compile time. The compiler will decide which method to call at runtime and if no method is found then it throws an error.
- public class Drawing
- {
- public virtual double Area()
- {
- return 0;
- }
- }
- public class Circle : Drawing
- {
- public double Radius { get; set; }
- public Circle()
- {
- Radius = 5;
- }
- public override double Area()
- {
- return (3.14) * Math.Pow(Radius, 2);
- }
- }
- public class Square : Drawing
- {
- public double Length { get; set; }
- public Square()
- {
- Length = 6;
- }
- public override double Area()
- {
- return Math.Pow(Length, 2);
- }
- }
- public class Rectangle : Drawing
- {
- public double Height { get; set; }
- public double Width { get; set; }
- public Rectangle()
- {
- Height = 5.3;
- Width = 3.4;
- }
- public override double Area()
- {
- return Height * Width;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Drawing circle = new Circle();
- Console.WriteLine("Area :" + circle.Area());
- Drawing square = new Square();
- Console.WriteLine("Area :" + square.Area());
- Drawing rectangle = new Rectangle();
- Console.WriteLine("Area :" + rectangle.Area());
- }
- }