19 September 2013

Polymorphism in C# & Types of Polymorphism with Example in C#


Introduction : In this article I will explain polymorphism, compile time polymorphism and runtime polymorphism in c#.net with example.
Polymorphism
Polymorphism is one of the principle of object oriented programming. "Poly" means many and "morph" means forms hence the name polymorphism. Polymorphism also refered to as one name many forms or having one name with multiple functionality.
In simple words you can use same method name with different signature or same signature but in different class.So depending on a data type it processes objects differently and an ability to redefine methods for a derived classes.

Types of Polymorphism


·         Compile time polymorphism (or) Static Polymorphism (or) Early Binding (or) Overloading (or) static binding
·         Runtime polymorphism (or)  Dynamic Polymorphism (or) Late Binding (or) Overriding (or) dynamic binding

Compile time polymorphism
Compile time polymorphism  is also called as Static Polymorphism. In Compile time polymorphism  methods are overloaded  with same name but having different signatures.So it is called as method overloading.
Runtime polymorphism
Runtime polymorphism is also called as Dynamic polymorphism. In this type of polymorphism methods have the same name, same signature but different in the implementation.In Dynamic polymorphism methods are overridden so it also called as methodoverloading.

          During run time, Method overriding can be achieved by using inheritance principle and using "virtual" and "override" keyword. so this type of polymorphism can also be called as late binding.In late binding compiler doesn't know what kind of methods it has to call and which can be achieved only during the run time.so it is called as run time polymorphism.

Compile time polymorphism example

public class Class1
{
public void NumbersAdd(int a, int b)
{
Console.WriteLine(a + b);
}
public void NumbersAdd(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
}

            In above class we have two methods with same name but having different input parameters this is called method overloading or compile time polymorphism or early binding.
Runtime polymorphism example
//Base Class
public class Bclass
{
public virtual void Sample1()
{
Console.WriteLine("Base Class");
}
}
// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console.WriteLine("Derived Class");
}
}
// Using base and derived class
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DClass objDc = new DClass();
objDc.Sample1();
// calling the base class method
Bclass objBc = new DClass();
objBc.Sample1();
}
}
If we run above code we will get output like as shown below
Output
Derived Class
Derived Class

(or)

Example of Compile Time Polymorphism

Method Overloading

- Method with same name but with different arguments is called method overloading.
- Method Overloading forms compile-time polymorphism.

Example of Method Overloading:
class A1
{
void hello()
{ Console.WriteLine(“Hello”); }

void hello(string s)
{ Console.WriteLine(“Hello {0}”,s); }
}

Example of Run Time Polymorphism

Method Overriding

- Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
- Method overriding forms Run-time polymorphism.
- Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.
 Example of Method Overriding:

Class parent
{
virtual void hello()
{ Console.WriteLine(“Hello from Parent”); }
}

Class child : parent
{
override void hello()
{ Console.WriteLine(“Hello from Child”); }
}

static void main()
{
parent objParent = new child();
objParent.hello();
}

Output

Hello from Child.

No comments:

Post a Comment