C# Tutorial 17 - Polymorphism in C#
Polymorphism means to take more than one form and it fourth principle concept of Object Oriented Programming. "Poly" means many and "morph" means forms. Polymorphism allows us to have multiple methods with same name in a class and then to call one of these methods based on our criteria or requirements.
Types of Polymorphism
- Static or Compile Time Polymorphism
- Dynamic or Runtime Polymorphism
Static or Compile Time Polymorphism
Method overloading and Operator overloading are examples of static or compile time polymorphism. It is also referred as early binding. It is called compile time or static polymorphism because decision for selecting appropriate method for given requirements is taken at compile time. Number, Type and Order of parameters is checked for selection of appropriate method in method overloading. For methods, main concept in overloading is that methods with same name but different signature. Keep in mind that return type is not part of signature. We can acquire overloading of methods using three ways.
- Number of parameters
- Order of parameters
- Type of parameters
public class Calculate
{
{
public void Add(int a, int b)
{
Console.WriteLine("a + b: "+sum);
}
Console.WriteLine("a + b: "+sum);
}
// Numbers of parameters
public void Add(int a, int b, int c)
{
Console.WriteLine("Sum of a+b+c: "+sum);
}
{
Console.WriteLine("Sum of a+b+c: "+sum);
}
// Types of parameters
public void Add(int a, double b)
{
Console.WriteLine("Sum of a + b: "+sum);
}
{
Console.WriteLine("Sum of a + b: "+sum);
}
// Order of parameters
public void Add(double a, int b)
{
Console.WriteLine("Sum of a + b: "+sum);
}
{
Console.WriteLine("Sum of a + b: "+sum);
}
class Program
{
static void Main()
{
{
static void Main()
{
Calculate c = new Calculate();
c.Add(1, 2);
c.Add(1, 2, 3);
c.Add(1, 2);
c.Add(1, 2, 3);
c.Add(1, 2.8593);
c.Add(32.54, 1);
Console.ReadLine();
} }
Dynamic or Runtime Polymorphism
The key idea behind method overriding is that, method name and parameters are same but they have different implementation as per requirements. It is also called dynamic polymorphism or late binding. Usually its done when inheritance is implemented and both Parent and Child class have method with same name. For example you have class named Vehicle which have a method with name VehicleInfo() and it displays general information of Vehicle and there is another class with name Car and this class also have a method with name VehicleInfo(), then definition of method VehicleInfo() in Vehicle class will be override by method in Car class. See example below. Hello method in Student, Employee and Human class will have its own definition and when using object of each class, each of object will call its respective class' method.
class Human
{
public virtual void Hello()
{
Console.WriteLine("Hello Human");
}
}
class Student: Human
{
public override void Hello()
{
{
public virtual void Hello()
{
Console.WriteLine("Hello Human");
}
}
class Student: Human
{
public override void Hello()
{
Console.WriteLine("Hello Student");
}
}
class Employee: Human
{
public override void Hello()
{
Console.WriteLine("Hello Human");
}
}
class Employee: Human
{
public override void Hello()
{
Console.WriteLine("Hello Human");
}
}
}
class Program
{
public static void Main()
{
Human h = new Human();
{
public static void Main()
{
Human h = new Human();
h.Hello(); // Output: Hello Human
Student s = new Student();
s.Hello(); // Output: Hello Student
Employee e = new Employee();
e.Hello(); // Output: Hello Employee
}
}
No comments:
Post a Comment