Implementing Dynamic Polymorphism i.e. Function Overriding In C#.Net



  • Function overriding is providing new Implementation to a function with the signature
  • In general function overriding will be implemented within the derived class
  • Let us consider a company asked to computerize Employee details, then we design a class i.e. ClsEmployee Like
  • During the period of time, Company has expanded and no of employees in the company are expanded to large, then company decided to categories the employees as managers
  • Further decided not to accept EAddress and EAge for Managers and to provide additional Car Allowance and Bonus
  • For non-managers we can use ClsEmployee class as it is, but cannot be used for managers
  • So, we will derive a new class from ClsEmployee, as ClsManager and provide new functionality for GetEmpData( ) and DisplayEmpData( ) functions
  • As we are providing new functionality for base class functions in derived class and base class functions are not useful now so base class functions are known as virtual functions and derived class are known as overriding functions
  • To make any function as virtual use virtual keyword
  • To override any virtual function in derived class use override keyword
 
Example :-





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

namespace CAFOverride
{
    class ClsEmployee
    {
        protected int EmpId, EAge;
        protected string EName, EAddress;
        public virtual void GetEmpData()
        {
            Console.Write("Enter Employee Details");
            Console.WriteLine("Enter Employee Id");
            this.EmpId = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Employee Name");
            this.EName = Console.ReadLine();
            Console.WriteLine("Enter Employee Address");
            this.EAddress = Console.ReadLine();
            Console.WriteLine("Enter Employee Age");
            this.EAge = Convert.ToInt32(Console.ReadLine());
        }
        public virtual void DisplayEmpData()
        {
            Console.WriteLine("Employee id is       :-  " + this.EmpId);
            Console.WriteLine("Employee Name is     :-  " + this.EName);
            Console.WriteLine("Employee Address is  :-  " + this.EAddress);
            Console.WriteLine("Employee Age is      :-  " + this.EAge);
        }
    }
    class ClsManager : ClsEmployee
    {
        double Bonus, CA;
        public override void GetEmpData()
        {
            Console.Write("Enter Manager Details");
            Console.WriteLine("Enter Manager id");
            EmpId = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Manager Name");
            EName = Console.ReadLine();
            Console.WriteLine("Enter Manager Bonus");
            Bonus = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter Manager CA");
            CA = Convert.ToDouble(Console.ReadLine());

        }
        public override void DisplayEmpData()
        {
            Console.WriteLine("Manager Id is    :-  " + EmpId);
            Console.WriteLine("Manager Name is  :-  " + EName);
            Console.WriteLine("Manager Bonus    :-  " + Bonus);
            Console.WriteLine("Manager CA       :-  " + CA);
        }
    }
    class ClsFOverride
    {
        static void Main(string[] args)
        {
            ClsManager Obj1 = new ClsManager();
            Obj1.GetEmpData();
            Obj1.DisplayEmpData();
            Console.Read();
        }
    }
}



Output:-


  • Overriding of virtual function is not compulsory, optional only
  • If base class virtual function is not overridden within the derived class then base class virtual function is called
  • We can instantiate / create an object to a class which contains one (or) more virtual functions
  • Creating a new class from a class which contains virtual functions is not compulsory, optional

Difference between function overloading and function overriding

SNo
Function Overloading
Function Overriding
01
Providing New Implementation to a function with same Name and Different Signature is known as Function Overloading
Providing New Implementation to a function with same Name and Same Signature is known as Function overriding
02
Function Overloading will be done in the same class
Function Overriding will be done in the Base and Derived Classes
03
This is Code refinement Technique
This is Code Replacement Technique
04
No separate keywords are used to implement function Overloading
Use virtual keyword for base class function and override keyword in derived class function to implement function overriding
05
Used to implement static polymorphism
Used to implement dynamic polymorphism





1 comment: