# Dynamic Polymorphism using Interface

Posted by Hmanjarawala under C# category on | Points: 40 | Views : 3297

namespace SampleApp
{
interface IShape
{
double Area();
}

class Circle : IShape
{
double r;

public Circle(double radious) { r = radious; }

public double Area()
{
return 3.14D * r * r;
}
}

class Rectangle : IShape
{
double l, w;

public Rectangle(double length) { l = length; w = length; }
public Rectangle(double length, double width) { l = length; w = width; }

public double Area()
{
return l * w;
}
}

class Triangle : IShape
{
double s1, s2, s3;

public Triangle(double side1, double side2, double side3) { s1 = side1; s2 = side2; s3 = side3; }

public double Area()
{
double a = (s1 + s2 + s3) / 2;
return Math.Pow((a * (a - s1) * (a - s2) * (a - s3)), 0.5D);
}
}

class RightAngle : IShape
{
double h, b;

public RightAngle(double b, double height) { this.b = b; h = height; }

public double Area()
{
return 0.5D * h * b;
}
}

class Cylinder : IShape
{
double r, h;

public Cylinder(double redious, double height) { r = redious; h = height; }

public double Area()
{
return 2 * 3.14D * r * r * h;
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
IShape s = new Circle(25);
System.Diagnostics.Debug.Print("Area of Circle: {0}", s.Area());

s = new Rectangle(12);
System.Diagnostics.Debug.Print("Area of Square: {0}", s.Area());

s = new Rectangle(12, 3.5D);
System.Diagnostics.Debug.Print("Area of Rectangle: {0}", s.Area());

s = new Triangle(10, 11, 12);
System.Diagnostics.Debug.Print("Area of Triangle: {0}", s.Area());

s = new RightAngle(3, 4);
System.Diagnostics.Debug.Print("Area of Rightangle: {0}", s.Area());

s = new Cylinder(3, 4);
System.Diagnostics.Debug.Print("Area of Cylinder: {0}", s.Area());
}

}
}

#### Comments or Responses

Latest Code Snipptes