Showing posts with label Runtime polymorphism and compiletime polymorphism. Show all posts
Showing posts with label Runtime polymorphism and compiletime polymorphism. Show all posts

Tuesday, April 9, 2013

Runtime polymorphism and compiletime polymorphism

Polymorphism is one of type of oops I.e object oriented programming. It says that Polymorphism is just a method with same name showing one to many characteristics.

For example method with same names in a class but with different no of parameters values exhibit different behavior for the same method name.

shape(int x)
shape(int x,int y)

Types of Polymorphism:


Compiletime Polymorphism :
It is known as method overloading

Example:
Class area
{
 Public calcArea (int x)
 {
 }
Public calcArea (int x, int y)
 {
 }
Public calcArea (int x, int y, int z)
 {
 }
}

Runtime polymorphism:
It is known as method overriding.

Example:
class Class2 
    {
        static void Main(string[] args)
        {
            Area a = new Area2();
            int x = a.calcArea(5);

            Console.WriteLine("Result" + x);
            Console.ReadLine();
        }
    }

    class Area2 : Area
    {
         public override int calcArea(int x)
         {
             return x * x;
         }

    }

    class Area
    {
        public virtual int calcArea(int x)
        {
            return x + x;
        }
    }