In this article we are going to discuss the Abstract class in C#. There are many blogs, articles are available on the internet regarding Abstract class but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Abstract class in C#.
Abstract Class Introduction
Abstract classes are useful when you want to define a common structure and behavior for related classes, enforce implementation of certain methods, promote code reuse, and achieve polymorphism. These are the some important points related to the Abstract Class.
The following class definition defines a abstract class in C#:
//abstract class
abstract class AbstractClass
{
}
Why Abstract Classes?
Abstract classes in C# are like blueprints for other classes. Imagine you're
building different types of cars, like sports cars and trucks. You'd have a
basic plan that includes things like wheels, engine, and seats, right? That's an
abstract class. It's a plan that says, any car made from this blueprint must
have these basic parts.
But here's the twist, you can't actually build a car directly from this
blueprint. It's just a template. You need to create a specific type of car, like
a sports car or a truck, based on this blueprint. Those specific cars are called
concrete classes. They take the abstract plan and fill in the details to make a
real car.
So, abstract classes are handy because they provide a common set of features
that all related classes can share. They help keep things organized and make
sure every class that follows the blueprint has the same basic structure.
Reason to Use Abstract Class
Typically, you would use a Abstract class when you want to prevent further derivation for one of two reasons:
For Example
Here we create a abstract class shape and inherit that class from derived class circle and Rectangle.
using System;
// Abstract class representing a basic shape
abstract class Shape
{
// Abstract method to calculate area
public abstract double CalculateArea();
}
// Concrete class representing a circle
class Circle : Shape
{
public double Radius { get; set; }
public Circle(double radius)
{
Radius = radius;
}
// Implementation of CalculateArea for circle
public override double CalculateArea()
{
return Math.PI * Radius * Radius;
}
}
// Concrete class representing a rectangle
class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
public Rectangle(double width, double height)
{
Width = width;
Height = height;
}
// Implementation of CalculateArea for rectangle
public override double CalculateArea()
{
return Width * Height;
}
}
class Program
{
static void Main(string[] args)
{
// Creating instances of circle and rectangle
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 6);
// Calculating and printing areas
Console.WriteLine($"Area of circle: {circle.CalculateArea()}");
Console.WriteLine($"Area of rectangle: {rectangle.CalculateArea()}");
}
}
In the Main method, we create instances of Circle and Rectangle, and we can call the CalculateArea() method on each instance to get their respective areas.