In object oriented programming, Polymorphism is a programming language ability to represent object in different forms and redefine methods in derived class from base class.
C# Polymorphism is a Greek word which means having "many forms". It is considered as one of strong pillar of object oriented programming.
Types of Polymorphism
Compile time Polymorphism – Static Polymorphism (Static Binding, Early Binding)
Virtual method allows declare a method in base class that can be redefined in each derived class. It is a method whose behavior can be overridden in derived class.
Note
Virtual keyword cannot be static, override, private or abstract.
By defaults methods are non-virtual.
Virtual method can be overridden in child class using override keyword.
Sealed Keyword
Description
Sealed keyword is used to stop method overriding in a derived classes.
Base classes may define and implement virtual methods and derived classes can override them, which means they provide their own definition and implementation.
Compile time Polymorphism
Description
Method overloading is achieved by this type of polymorphism. It is also called as Static polymorphism. Here compiler checks the data type and number of parameters passed on to the method and decides which method to call at compile time .Compiler gives an error if there are no methods that match the method signature of the method that is called at compile time.
To implement Compile time polymorphism below are the two techniques:
Operator Overloading
Function Overloading
Example
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SPLessons
{
class Program
{
static void Main(string[] args)
{
ClassX obj = new ClassX();
int c = obj.NumbersAdd(10, 10);
int d = obj.NumbersAdd(10, 10, 20);
Console.WriteLine(c);
Console.WriteLine(d);
Console.ReadLine();
}
public class ClassX
{
public int NumbersAdd(int a, int b)
{
return a + b;
}
public int NumbersAdd(int a, int b, int c)
{
return a + b + c;
}
}
}
}
[/csharp]
Output:
Runtime Polymorphism
Description
Method overriding is achieved by this type of polymorphism. At run time it will be decided which method to be called and if no method found it gives an error .
Runtime polymorphism is implemented by abstract classes and virtual functions.
Example
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SPLessons
{
class Program
{
static void Main(string[] args)
{
System.Collections.Generic.List<Shape> shapes = new System.Collections.Generic.List<Shape>();
shapes.Add(new Rectangle());
shapes.Add(new Circle());
foreach (Shape s in shapes)
{
s.Draw();
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
public class Shape
{
public int X { get; private set; }
public int Y { get; private set; }
public int Height { get; set; }
public int Width { get; set; }
// Virtual method
public virtual void Draw()
{
Console.WriteLine("Base class draw Method");
}
}
// Extends base class shape and override draw method of base circle...
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Child class - circle");
base.Draw();
}
}
// Extends base class shape and override draw method of base circle...
class Rectangle : Shape
{
public override void Draw()
{
// Code to draw a rectangle...
Console.WriteLine("Child class - rectangle");
base.Draw();
}
}
}
}
[/csharp]
Output: