Introduction

An abstract base class (ABC) is a concept in object-oriented programming and design that serves as a blueprint for other classes. An abstract class cannot be instantiated directly; it exists solely to be subclassed by other classes, which will then inherit its structure and behavior. Abstract base classes provide a way to define common interfaces and attributes that derived classes must implement or override. This approach promotes code reusability and ensures that certain methods or attributes are present in all subclasses.

Defined Abstract base class

In C++, an abstract base class (ABC) is a class that is designed to serve as a base or parent class for other classes but cannot be instantiated on its own. The primary purpose of an abstract base class is to provide a common interface or contract for its derived classes, defining a set of pure virtual functions that must be implemented by the derived classes.

An abstract base class contains one or more pure virtual functions, which are declared with the “= 0” syntax and have no implementation in the base class. These pure virtual functions act as placeholders for functionality that must be implemented by the derived classes.

Key points to defined the abstract base class-

Pure Virtual Functions: An abstract base class contains one or more pure virtual functions. A pure virtual function is declared with the syntax –

virtual returnType functionName(parameters) = 0;

Unlike regular virtual functions, pure virtual functions have no implementation in the abstract base class. They only serve as placeholders for functionality that must be implemented by derived classes.

Polymorphism: The use of abstract base classes enables polymorphism. Pointers and references of the abstract base class type can be used to refer to derived class objects, allowing for runtime polymorphic behavior. This is one of the key features of object-oriented programming in C++.

Interface Definition: The abstract base class provides an interface that outlines the functions and their signatures that must be implemented by the derived classes. It defines a common contract that ensures consistent behavior among all derived classes.

Inability to Instantiate: Since the abstract base class has one or more pure virtual functions without implementations, objects of the abstract class cannot be created. Attempting to create an instance of the abstract base class will result in a compilation error.

Characteristics of abstract base class-

characteristics of abstract base classes:

Code reusability: Abstract base classes promote code reuse by allowing multiple subclasses to share the common functionality defined in the base class.

Encapsulation: Abstract base classes encapsulate common functionality and hide the details of individual subclasses, providing a clean separation of concerns.

Abstraction and specialization: Abstract base classes promote the idea of abstraction, where you define the common aspects of a group of related classes while leaving the specialized details to individual subclasses.

Incomplete functionality: An abstract base class cannot be instantiated directly because it lacks full implementations for its methods. It may contain some concrete methods with implementations, but at least one of its member functions must be a pure virtual function.

Common interface: The abstract base class defines a common interface for all its derived classes. This interface ensures that all derived classes share a set of functions, making it easier to work with objects of different classes through a shared interface.

Polymorphism: By using abstract base classes, you can achieve polymorphism, which allows you to treat objects of different classes in a uniform way. You can create pointers or references to the abstract base class type and use them to call common interface methods on objects of derived classes.

Example of Abstract Base Class

#include <iostream>

// Abstract base class
class Shape {
public:
    // Pure virtual function
    virtual double calculateArea() const = 0;

    // Regular function with implementation
    void printArea() const {
        std::cout << "Area: " << calculateArea() << std::endl;
    }
};

// Derived class Circle
class Circle : public Shape {
private:
    double radius;

public:
    Circle(double r) : radius(r) {}

    // Implementation of the pure virtual function
    double calculateArea() const override {
        return 3.14159 * radius * radius;
    }
};

// Derived class Rectangle
class Rectangle : public Shape {
private:
    double width, height;

public:
    Rectangle(double w, double h) : width(w), height(h) {}

    // Implementation of the pure virtual function
    double calculateArea() const override {
        return width * height;
    }
};

int main() {
    // Attempting to create an instance of the abstract class will result in a compilation error.
    // Shape shape; // Error: cannot instantiate abstract class

    Circle circle(5.0);
    Rectangle rectangle(4.0, 6.0);

    // Polymorphism using base class pointers
    Shape* shapePtr = &circle;
    shapePtr->printArea(); // Output: Area: 78.5398

    shapePtr = &rectangle;
    shapePtr->printArea(); // Output: Area: 24

    return 0;
}

In this example, the Shape class is an abstract base class, and Circle and Rectangle are derived classes that inherit from Shape. They both provide implementations for the pure virtual function calculateArea(), which is required to satisfy the contract defined by the Shape class.

Conclusion-

In many programming languages, like C++, Java, and Python, abstract base classes provide a way to define interfaces or common contracts for classes, but they cannot be instantiated directly. Instead, they serve as blueprints for creating subclasses that implement the abstract methods.

Overall, abstract base classes are a powerful tool for creating well-structured and maintainable code by enforcing a consistent structure and behavior across a group of related classes while allowing specialized functionality in individual subclasses.


more related content on Object Oriented Programming

JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.