What are abstract base classes?

In object-oriented programming (OOP), abstract base classes are the classes that do not contain function implementations. They only contain abstract functions that provide the footprint for derived classes.

An abstract base class must contain at least one abstract function. All derived classes inheriting from it must provide their implementations for those functions. Abstract base classes are implemented differently in different programming languages.

Abstract base classes in C++

In C++, an abstract base class is a base class that has at least one pure virtual function. We cannot create an object of an abstract base class. For example, the following code throws an error:

#include <iostream>
using namespace std;
class abstractBaseClass {
public:
virtual void sayHello() = 0; // Pure virtual class
};
int main() {
// The following line throws an error
// because we cannot create objects of
// abstract classes
abstractBaseClass anObject = abstractBaseClass();
}

The pure virtual function must be defined in a child class, or else the compiler throws an error. In case we don't define it in a derived class, the compiler treats it as an abstract class that doesn't allow us to create objects of that particular class.

Code

#include <iostream>
using namespace std;
class abstractBaseClass {
public:
virtual void sayHello() = 0; // Pure virtual function
};
class derivedClass: abstractBaseClass {
public:
void sayHello() {
cout << "Hello! I am a derived class!" << endl;
}
};
int main() {
derivedClass anObject = derivedClass();
anObject.sayHello();
}

Explanation

  • Lines 4–7: We define an abstract base class that has a pure virtual function called sayHello().

  • Lines 8–14: We define another class is defined that inherits from the abstractBaseClass. It provides a definition for the pure virtual function sayHello().

  • Lines 16–19: We create an object of derivedClass and then call the sayHello() function.

Abstract base classes in Python

Abstract base classes work similarly in Python as they do in C++, except for the syntax differences. In Python, we need to inherit from abc.ABC and import abc.abstractmethod to work with abstract base classes. Similar to C++, we must define an abstract function in a derived class, or it throws an error.

Code

from abc import ABC, abstractmethod
# Abstract base class
class anAbstractClass(ABC):
@abstractmethod
def sayHello(self): # Abstract method
pass
# Derived class
class aDerivedClass(anAbstractClass):
def sayHello(self): # Definition of abstract method
print("Hello! This is a derived class!\n")
class anotherDerivedClass(anAbstractClass):
def diffHello(self):
print("Hello! This is the second derived class!\n")
try:
object1 = anAbstractClass()
print("Success! An object of the abstract base class is sucessfully created!\n")
except:
print("Error! Cannot create an object of abstract base class\n")
object2 = aDerivedClass()
object2.sayHello()
try:
object3 = anotherDerivedClass()
object3.diffHello()
except:
print("Error! Cannot create an object of 'anotherDerivedClass'")

Explanation

  • Lines 4–7: We define a base abstract class that contains an abstract function sayHello() specified by the @abstractmethod keyword.

  • Lines 10–12: We define a derived class that inherits from anAbstractBaseClass and provides the definition for the abstract sayHello() method.

  • Lines 14–16: We define another derived class that inherits from anAbstractBaseClass, but don't define the sayHello() method.

  • Lines 18–31: We use try and except to try creating instances of class objects, and print the appropriate error message in case of success or failure.

Copyright ©2024 Educative, Inc. All rights reserved