Polymorphism in C# : Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Shape
{
protected int width, height;
public Shape()
{
}
public virtual void area(int width, int height)
{
Console.WriteLine("Parent class area :");
Console.WriteLine("Shape has no spasific area ");
}
}
class Rectangle : Shape
{
public Rectangle() : base()
{
}
public override void area(int width, int height)
{
Console.WriteLine("Rectangle class area :");
Console.WriteLine(width * height);
}
}
class Triangle : Shape
{
public Triangle() : base()
{
}
public new void area(int width,int height)
{
Console.WriteLine("Triangle class area :");
Console.WriteLine(width * height / 2);
}
}
class Tester
{
static void Main(string[] args)
{
Shape s = new Shape();
Rectangle r = new Rectangle();
Triangle t = new Triangle();
s.area(0,0);
r.area(10,7);
t.area(10,5);
Shape s1 = new Rectangle();
s1.area(10, 15);
Shape s2 = new Triangle();
s2.area(22, 76);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Shape
{
protected int width, height;
public Shape()
{
}
public virtual void area(int width, int height)
{
Console.WriteLine("Parent class area :");
Console.WriteLine("Shape has no spasific area ");
}
}
class Rectangle : Shape
{
public Rectangle() : base()
{
}
public override void area(int width, int height)
{
Console.WriteLine("Rectangle class area :");
Console.WriteLine(width * height);
}
}
class Triangle : Shape
{
public Triangle() : base()
{
}
public new void area(int width,int height)
{
Console.WriteLine("Triangle class area :");
Console.WriteLine(width * height / 2);
}
}
class Tester
{
static void Main(string[] args)
{
Shape s = new Shape();
Rectangle r = new Rectangle();
Triangle t = new Triangle();
s.area(0,0);
r.area(10,7);
t.area(10,5);
Shape s1 = new Rectangle();
s1.area(10, 15);
Shape s2 = new Triangle();
s2.area(22, 76);
Console.ReadKey();
}
}
}
Post a Comment