What is abstract class and why we need of it?
An abstract class in object-oriented programming (OOP) is a class that cannot be instantiated on its own and is meant to serve as a blueprint or template for other classes. It may contain abstract methods (methods without implementations) that must be implemented by its derived classes. Abstract classes provide a way to define common attributes and behaviors that should be shared by multiple related classes.
Here's why we need abstract classes:
-
Common Base with Shared Characteristics: Abstract classes allow you to define a common base with shared attributes and methods for a group of related classes. This helps in maintaining a consistent structure and behavior across the derived classes.
-
Forcing Implementation: Abstract classes can declare abstract methods, which are methods without a body. Derived classes that inherit from the abstract class must provide concrete implementations for these abstract methods. This ensures that certain methods are implemented consistently across all derived classes.
-
Encapsulation of Common Logic: Abstract classes can contain concrete methods (methods with implementations) along with abstract methods. This means that common logic can be encapsulated in the abstract class, and derived classes only need to implement the unique or specialized parts.
-
Polymorphism and Inheritance: Abstract classes support polymorphism, allowing instances of derived classes to be treated as instances of the abstract base class. This promotes code reusability and the ability to work with multiple derived classes through a common interface.
-
Limiting Instantiation: Abstract classes cannot be instantiated directly. This is useful when you want to ensure that certain classes are only used as base classes and not directly as objects.
-
Guiding Design and Structure: Abstract classes provide a higher-level design structure that guides the development of derived classes. This helps in maintaining a consistent design and architecture for related classes.
Here's a simple example to illustrate an abstract class in C#:
abstract class Shape {
public abstract double CalculateArea(); // Abstract method without implementation
}
class Circle : Shape {
public double Radius { get; set; }
public Circle(double radius) {
Radius = radius;
}
public override double CalculateArea() {
return Math.PI * Radius * Radius;
}
}
class Rectangle : Shape {
public double Width { get; set; }
public double Height { get; set; }
public Rectangle(double width, double height) {
Width = width;
Height = height;
}
public override double CalculateArea() {
return Width * Height;
}
}
In this example, the 'Shape' abstract class defines an abstract method 'CalculateArea()'. The 'Circle' and 'Rectangle' classes inherit from 'Shape' and provide concrete implementations for the 'CalculateArea()' method. This allows for consistent calculation of areas while allowing different shapes to define their own specific implementation.
Below is another example which shows how the abstract classes are declared and implemented: