Bellow Article Explain how you can achieve Run-time Polymorphism
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Runtime_polymorphism
{
class Program
{
static void Main(string[] args)
{
/* Testing Polymorphism */
Shape[] s = new Shape[3];
/* Polymorphic Objects */
/* creating Array with Different types of Objects */
s[0] = new Circle();
s[1] = new Ractangel();
s[2] = new Traingle();
Console.WriteLine("\n\nRuntime polymorphism test\n\n");
for (int i = 0; i < 3; i++)
{
s[i].Draw();
}
Console.ReadKey();
}
}
-------------------------------------------------------------------------------------------------------------
class Shape
{
public virtual void Draw()
{
}
}
-------------------------------------------------------------------------------------------------------------
class Ractangel : Shape
{
public override void Draw()
{
Console.WriteLine("Rectangle Drawn ");
}
}
--------------------------------------------------------------------------------------------------------------
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Circle Drawn ");
}
}
-------------------------------------------------------------------------------------------------------------
class Traingle : Shape
{
public override void Draw()
{
Console.WriteLine("Triangle Drawn ");
}
}
}
No comments:
Post a Comment