We will create small console project to understand when to use Abstract class and Interface as follow.
Let's consider we need to create one small application which will draw the shape and clacualteArea of shape and calculate the diameter of the shape.
The following code will achieve the when to use the abstract class.
namespace WhenToUseAbstractClassAndInterface
{
public abstract class Shape
{
public string ShapeName { get; set; }
public void DrawShape()
{
Console.WriteLine("Draw shape is "+this.ShapeName);
}
public abstract double CalculateArea();
}
public class Rectangle:Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override double CalculateArea()
{
return this.Width * this.Height;
}
}
public class Circle:Shape
{
public double PI { get; set; }
public double Radius { get; set; }
public override double CalculateArea()
{
return this.PI *(this.Radius*this.Radius);
}
}
class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle()
{
ShapeName="Rectangle",
Width=10.10,
Height=12.1
};
rect.DrawShape();
Console.WriteLine("Area of Rectangle is {0}", rect.CalculateArea());
Circle circle = new Circle()
{
ShapeName = "Circle",
PI = 3.14,
Radius=10.1
};
circle.DrawShape();
Console.WriteLine("Area of Circle is {0}", circle.CalculateArea());
Console.ReadLine();
}
}
}
So we have come new enhancement in the above application. we want to implement the calculate the Diameter of a circle.
So to implement this functionality we need to declare the separate interface. and inherits in circle class.
Why because the calculating diameter is only required for circle class, not for other classes as follow.
Create the interface for calculating diameter as follow
interface ICircle
{
double CalculateDiameter();
}
Inherits to the Circle class only.
Full application code with abstract class and interface as follow.
Full application code when to use abstract class and interface in c#.
using System;
namespace WhenToUseAbstractClassAndInterface
{
interface ICircle
{
double CalculateDiameter();
}
public abstract class Shape
{
public string ShapeName { get; set; }
public void DrawShape()
{
Console.WriteLine("Draw shape is "+this.ShapeName);
}
public abstract double CalculateArea();
}
public class Rectangle:Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override double CalculateArea()
{
return this.Width * this.Height;
}
}
public class Circle:Shape,ICircle
{
public double PI { get; set; }
public double Radius { get; set; }
public double CalculateDiameter()
{
return 2 * (this.Radius);
}
public override double CalculateArea()
{
return this.PI *(this.Radius*this.Radius);
}
}
class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle()
{
ShapeName="Rectangle",
Width=10.10,
Height=12.1
};
rect.DrawShape();
Console.WriteLine("Area of Rectangle is {0}", rect.CalculateArea());
Circle circle = new Circle()
{
ShapeName = "Circle",
PI = 3.14,
Radius=10.1
};
circle.DrawShape();
Console.WriteLine("Area of Circle is {0}", circle.CalculateArea());
Console.WriteLine("Diameter of Circle is {0}", circle.CalculateDiameter());
Console.ReadLine();
}
}
}
I hope this will help you to understand when to use abstract class and when to use interface.