Tuesday, March 6, 2012

Polymorphism Via Inheritance

In one of my post  we discussed four basic tenets of Object Oriented Programming. Polymorphism is one of the key fundamental of OOP. Polymorphism means many forms - in other words an object can take on many forms based on its implementation details. There are several ways you can implement polymorphism such as via inheritance, via interface or via abstract classes. We will discuss polymorphism via inheritance today.

Consider a base employee class with few properties and then you create a full time and part time employee classes that inherit from the employee class.

Basically you are declaring a base class with some parameters/features and then extending it into the derived classes by either modifying the base functionality or adding class specific features.

Base Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Polymorphism
{
    class Employee
    {
        public int employeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual void PrintEmployee()
        {
            Console.WriteLine("EmployeeID={0},Employee Name={1}, {2}", 
                              employeeID,LastName, FirstName);
            Console.WriteLine(Environment.NewLine);
        }
    }
}


Derived Class 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Polymorphism
{
    class FullTime:Employee
    {
        public string EmployeeType { get; set; }
        public string OverTime { get; set; }
        public string VacationDays {get;set;}
  
       public override void PrintEmployee()
        {
            base.PrintEmployee();
            Console.WriteLine("Employee Type={0}, OverTime={1}, VacationDays{2}", 
                              EmployeeType, OverTime, VacationDays);
            Console.WriteLine(Environment.NewLine);
        }
    }
}



Derived Class 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Polymorphism
{
    class PartTime:Employee
    {
        public string EmployeeType { get; set; }
        public string OverTime { get; set; }
        public string VacationDays {get;set;}
  
       public override void PrintEmployee()
        {
            base.PrintEmployee();
            Console.WriteLine("Employee Type={0}, OverTime={1}, VacationDays{2}", 
                              EmployeeType, OverTime, VacationDays);
            Console.WriteLine(Environment.NewLine);
        }
    }
}



As you can see, base virtual method "PrintEmployee" is extended in derived classes. Properties declared in the base class are available to derived classes.

If you want to extend a method like we did here, you must declare the method with keyword "virtual" in base class and use keyword "override" in derived class.

This is a very basic concept but could be confusing for someone new to object oriented programming paradigm. Hope this helps you understand the concept.

In future posts we will discuss polymorphism via interface and abstract classes.

Thank you.



No comments:

Post a Comment