When do you use abstract class in C#?
When there is requirement that your base class has implementation of the few common methods and the remaining method need to implement in the child classes according to business requirement of the child classes then we use abstract class. When we actually don’t know at design time what will actually happen during run time.
In other words abstract classes in C# are used when you want to create a common base class that provides a blueprint for derived classes but cannot be instantiated on its own. The main scenarios where you would use abstract classes are:
-
Providing a common base:
-
Abstract classes are used to define a common set of methods, properties, and fields that derived classes should implement or inherit.
-
They serve as a blueprint or contract for derived classes, ensuring a consistent structure and behavior.
-
Abstract classes provide a way to establish a common base with shared functionality, reducing code duplication.
-
Enforcing implementation:
-
Abstract classes can contain abstract methods, which are methods without an implementation.
-
Derived classes must provide an implementation for all abstract methods defined in the abstract class.
-
This allows you to enforce specific behavior or functionality in derived classes.
-
Polymorphism:
-
Abstract classes enable polymorphism, which allows you to treat derived objects as instances of the abstract base class.
-
This enables you to write code that operates on the base class and can be reused for different derived classes.
-
Polymorphism allows you to create more flexible and extensible code.
-
Incomplete implementation:
-
Abstract classes can have both abstract and non-abstract methods.
-
This allows you to provide a partial implementation in the abstract class while leaving some methods to be implemented by derived classes.
-
Abstract classes can provide default behavior or common functionality that derived classes can use and build upon.
Here's an example to illustrate the use of an abstract class:
abstract class Shape
{
public abstract double CalculateArea();
public void Display()
{
Console.WriteLine("This is a shape.");
}
}
class Circle : Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public override double CalculateArea()
{
return Math.PI * radius * radius;
}
}
class Rectangle : Shape
{
private double length;
private double width;
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
public override double CalculateArea()
{
return length * width;
}
}
class Program
{
static void Main()
{
Circle circle = new Circle(5);
Console.WriteLine("Circle Area: " + circle.CalculateArea());
Rectangle rectangle = new Rectangle(4, 6);
Console.WriteLine("Rectangle Area: " + rectangle.CalculateArea());
}
}
In this example, the abstract class 'Shape' defines the blueprint for shapes and declares an abstract method 'CalculateArea()' that derived classes must implement. The 'Shape' class also provides a non-abstract method 'Display()' with a default implementation.
The derived classes 'Circle' and 'Rectangle' inherit from the 'Shape' class and provide their own implementations for the 'CalculateArea()' method. They can also utilize the 'Display()' method inherited from the base class.
By using the abstract class 'Shape', we ensure that any shape derived from it must provide its own implementation of the 'CalculateArea()' method, while also allowing us to treat different shapes as instances of the 'Shape' class.
To summarize, you use abstract classes in C# to define a common base with shared functionality, enforce implementation in derived classes, enable polymorphism, and provide a partial or default implementation.