What is polymorphism. How is it related to early and late binding.


=> Polymorphism is the ability to implement the same operation many times.

=> A method called indirectly through a base class operation is bounded at run time. This is called late or dynamic binding. 

=> A method called directly ( not through a base class operation ) is bounded at compile time. This is called early or static binding.

Example :-

using System;

public class Employee
{
    public string FirstName = "Praveen";
    public string LastName = "Singh";

    public virtual void PrintFullName()
    {
        Console.WriteLine(FirstName + " " + LastName);
    }

}

public class PartTimeEmployee:Employee
{
    public override void PrintFullName()
    {
        Console.WriteLine(FirstName + " " + LastName + " -PartTimeEmployee");
    }
}
public class FullTimeEmployee:Employee
{
    public override void PrintFullName()
    {
        Console.WriteLine(FirstName + " " + LastName + " -FullTimeEmployee");
    }
}
public class TemproryEmployee:Employee
{
    public override void PrintFullName()
    {
        Console.WriteLine(FirstName + " " + LastName + " -TemproryEmployee");
    }
}
    class Program
    {
        static void Main(string[] args)
        {
            Employee[] employee = new Employee[4];
            employee[0] = new Employee();
            employee[1] = new PartTimeEmployee();
            employee[2] = new FullTimeEmployee();
            employee[3] = new TemproryEmployee();

            foreach(Employee e in employee)
            {
                e.PrintFullName();
            }
            Console.ReadLine();
        }
    }

OUTPUT

Praveen Singh
Praveen Singh -PartTimeEmployee
Praveen Singh -FullTimeEmployee
Praveen Singh -TemproryEmployee

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2019 HART™ is a registered trademark.