Abstract Classes and Abstract Methods in C#

An abstract class in C# is a class that cannot be instantiated on its own and serves as a blueprint for other classes. It allows you to define common methods, properties, and fields that derived classes must implement.

Abstract Class Declaration in C#

To declare an abstract class, use the abstract keyword.

abstract class Shape { public abstract double CalculateArea(); // Abstract method public abstract double CalculatePerimeter(); // Abstract method public void DisplayDetails() { // Concrete method Console.WriteLine($"Area: {CalculateArea()}, Perimeter: {CalculatePerimeter()}"); } }

In this example, Shape is an abstract class with two abstract methods (CalculateArea and CalculatePerimeter) that must be implemented by derived classes. It also has a concrete method, DisplayDetails, that can be inherited as is.

Abstract Method

Abstract methods have no implementation in the abstract class and are marked with the abstract keyword. Derived classes must provide a concrete implementation for all abstract methods.

Derived Class Implementation

When creating a derived class from an abstract class, you must provide implementations for all abstract methods.

class Circle : Shape { private double radius; public Circle(double r) { radius = r; } public override double CalculateArea() { return Math.PI * radius * radius; } public override double CalculatePerimeter() { return 2 * Math.PI * radius; } }

In this example, Circle is a derived class from the Shape abstract class. It provides concrete implementations for CalculateArea and CalculatePerimeter.

Instantiating Concrete Classes

While you cannot create an instance of an abstract class, you can create instances of concrete classes derived from it.

Shape circle = new Circle(5.0); circle.DisplayDetails(); // Calls methods from Circle class

Here, we create an instance of the Circle class and store it in a variable of type Shape.

Benefits of Abstract Classes

  1. Improved code organization: Abstract classes can help you to organize your code by defining a common interface for a set of classes. This can make your code more readable and maintainable.
  2. Improved code reuse: Abstract classes can help you to reuse code by allowing you to implement common functionality in the abstract class and then inherit from the abstract class to implement the specific functionality for each class.
  3. Improved polymorphism: Abstract classes can help you to implement polymorphism by allowing you to override methods in derived classes. This allows you to implement different behaviors for the same method depending on the type of object.

Abstract Properties and Fields

In addition to abstract methods, abstract classes can also define abstract properties and fields that must be implemented by derived classes.

Conclusion

An abstract class in C# serves as a blueprint for other classes and cannot be instantiated on its own. It defines a contract by declaring abstract methods, properties, and fields that must be implemented by derived classes, promoting code reusability and enforcing a common structure for related classes. Abstract classes allow developers to create hierarchies of related classes while ensuring specific behaviors are implemented in subclasses.