Polymorphism in C# 5.0

Polymorphism is one of the primary concepts of Object Oriented Programming. There are basically two types of polymorphism (i) RunTime (ii) CompileTime. Overriding a virtual method from a parent class in a child class is RunTime polymorphism while CompileTime polymorphism consists of overloading identical functions with different signatures in the same class. This program depicts Run Time Polymorphism.

The method Calculate(int x) is first defined as a virtual function int he parent class & later redefined with the override keyword. Therefore, when the function is called, the override version of the method is used.


The example below shows the how we can implement polymorphism in C Sharp. There is a base class called PolyClass. This class has a virtual function Calculate(int x) . The function exists only virtually i.e. it has no real existence. The function is meant to redefined once again in each subclass. The redefined function gives accuracy in defining the behavior intended. Thus, the accuracy is achieved on a lower level of abstraction. The four subclasses namely : CalSquare, CalSqRoot, CalCube & CalCubeRoot give a different meaning to the same named function Calculate(int x). When we initialize objects of the base class, we specify the type of object to be created.

namespace Polymorphism
{
    public class PolyClass
    {
        public virtual void Calculate(int x)
        {
            Console.WriteLine("Square Or Cube ?? Which One ??");
        }
    }

    public class CalSquare : PolyClass
    {
        public override void Calculate(int x)
        {
            Console.WriteLine("Square :  ");
            //Calculate the Square  of a number

            Console.WriteLine(Convert.ToString(Convert.ToInt64(x * x ) ));
        }
    }

    public class CalSqRoot : PolyClass
    {
        public override void Calculate(int x)
        {
            Console.WriteLine("Square Root Is :  ");           
            //Calculate the Square Root of a number
           
            Console.WriteLine(Convert.ToString(Convert.ToInt64(Math.Sqrt( x))));
        }
    }

    public class CalCube : PolyClass
    {
        public override void Calculate(int x)
        {
            Console.WriteLine("Cube Is :  ");           
            //Calculate the cube of a number
           
            Console.WriteLine(Convert.ToString(Convert.ToInt64(x * x * x)));
        }
    }

    public class CalCubeRoot : PolyClass
    {
        public override void Calculate(int x)
        {
            Console.WriteLine("Cube Square Is :  ");

            //Calculate the Cube Root of a number
           
            Console.WriteLine(Convert.ToString(Convert.ToInt64(Math.Pow(x,
            (0.3333333333333)))));
        }
    }
  
}


namespace Polymorphism
{
    class Program
    {
        static void Main(string[] args)
        {
            PolyClass[] CalObj = new PolyClass[4];
            int x;
            CalObj[0] = new CalSquare();
            CalObj[1] = new CalSqRoot();
            CalObj[2] = new CalCube();
            CalObj[3] = new CalCubeRoot();           

            foreach (PolyClass CalculateObj in CalObj)
            {
                Console.WriteLine("Enter Integer");
                x = Convert.ToInt32(Console.ReadLine());
                CalculateObj.Calculate( x );
               
            }
            Console.WriteLine("Aurevoir !");
            Console.ReadKey();
        }
    }
}


Please Note :
** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.5 but code should be very similar for previous versions of .Net
** All Program Codes here are  100%  tested & running.
Custom Search