Interface Implementation C# with Example



Interface Implementation C# with Example

 using System;

namespace CSharpInterface
{
	interface IShape
	{
		double area();
	}
	
    class Shape
    {
        public double dim1, dim2;

        public Shape(double dimension1, double dimension2)
        {
            dim1 = dimension1;
            dim2 = dimension2;
        }
    }
    class Rectangle : Shape, IShape
    {
        public Rectangle(double dimension1, double dimension2) : base(dimension1, dimension2) { }

        public double area()
        {
            Console.WriteLine("\nInside Area for Rectangle : ");
            return dim1 * dim2;
        }
    }

    class Triangle : Shape, IShape
    {
        public Triangle(double dimension1, double dimension2) : base(dimension1, dimension2) { }

        public double area()
        {
            Console.WriteLine("\nInside Area for Triangle : ");
            return dim1 * dim2 / 2;
        }
    }

    class AbstractClass
    {
        public static void Main(string[] args)
        {
            Rectangle newRectangle = new Rectangle(6, 7);
            Triangle newTriangle = new Triangle(12, 23);

            IShape referenceShape;

            referenceShape = newRectangle;
            Console.WriteLine("Area is " + referenceShape.area());

            referenceShape = newTriangle;
            Console.WriteLine("Area is " + referenceShape.area());
        }
    }
}
 

0 Comment's

Comment Form

Submit Comment