Abstract classes: Force a class to implement methods.
Abstract classes can contain abstract methods: methods without an implementation.
Objects cannot be created from an abstract class. A subclass can implement an abstract class.
Related course: Python Programming Courses & Exercises
Abstract methods
An abstract class has methods, but no implementation.
This is similar to an interface in other languages.
Abstract class cannot be instantiated and a sub class must implement it’s methods.
If you have many objects of a similar type, you can call them in a similar fashion.
The example below shows an abstract base class.
from abc import ABC, abstractmethod |
If you try to create an object, it throws an error:
obj = AbstractClassExample() |
This message would show:
Traceback (most recent call last): |
Classes can inherit from an abstract base class.
In this case, you could inherit from AbstractClassExample.
Every child class can imlement the methods differently.
class Example(AbstractClassExample): |
To summarize:
- An abstract base class cannot be instantated (no object creation)
- An abstract base class has methods but no implementation
- Sub classes can inherit from an abstract base class and implement methods
If you are a Python beginner, then I highly recommend this book.
Example
So why would you use Abstract Base Classes?
For one, your program becomes easy to extend.
Let’s say you extend a paint program and want to load a new image format.
Then all you would need to do, is implement methods from an abstract base class.
from abc import ABC, abstractmethod |
You can imagine every image format needs a different code.
Saving a bitmap will be different from saving webm format.
So every class can make its own implementation.
class Bitmap(Image):
def load_image(self,filename):
print('loading bitmap')
def save_image(self,filename):
print('save bitmap')
class Jpeg(Image):
def load_image(self,filename):
print('loading jpeg')
def save_image(self,filename):
print('saving jpeg')
Because they both inherit from the abstract base class, the structure is the same.
You can see the abstract base class as a blue print.
Second example
Imagine having classes like Truck, Car and Bus.
They would all have methods like start()
, stop()
, accelerate()
.
An abstract class (Automobile) can define these abstract methods but not implement them.
truck.start() |
Visually that looks like:
When a new class is added, a developer does not need to look for methods to implement. He/she can simply look at the abstract class.
If one of the sub classes (Truck, Car, Bus) misses an implementation, Python automatically throws an error.
If you are a Python beginner, then I highly recommend this book.
Abstract class example
Create an abstract class: AbstractAnimal. In the abstract class we only define the methods without an implementation.
You can then create concrete classes: classes containing an implementation. Let’s create a class Duck which implements the AbstractAnimal. We use the same methods, but now add an implementation.
import abc |
If we forget to implement one of the abstract methods, Python will throw an error.Traceback (most recent call last):
File "abst.py", line 27, in <module>
obj = Duck('duck1')
TypeError: Can't instantiate abstract class Duck with abstract methods walk
This is how you can force classes to have methods. Those methods need to be defined in the abstract class.
Summary
We can summarize this article in a few points:
- You can use an abstract base class to create a blue print.
- Objects cannot be created from an abstract base class.
- You can have one more sub classes that inherit from the abstract base class.
- In other programming languages, it is called an interface.
If you are a Python beginner, then I highly recommend this book.