1. Abstract Base Classes
Welcome back! In this video, we'll discuss abstract base classes, and how they're used when building other classes in Python.
2. Abstract base classes
Abstract base classes are tools used to create a "blueprint" for other classes. They do this by defining abstract methods that must be implemented by all classes that inherit the abstract base class. This helps to ensure common behavior across a group of classes.
To denote abstract methods, we'll use the abstractmethod decorator from the abc module; more on this to come.
Abstract base classes are only meant to be inherited, and are never instantiated themselves.
Not every method in an abstract base class needs to be abstract. These methods are known as concrete methods, and they're inherited like normal.
Let's say we were were defining a MiddleSchool and a HighSchool class; we'd want both to follow a similar "blueprint", such as allowing students to enroll, as well as add a course.
In the School abstract base class, we could define two abstract methods; enroll and add_course. Any class then wishing to inherit School (such as MiddleSchool or HighSchool) must provide it's own implementation of enroll and add_course.
Both classes would be free to define other methods, and may even inherit a concrete method from School.
3. Creating an abstract base class
Before we get started creating abstract base classes, we'll need to import the ABC class and the abstractmethod decorator from the abc module.
Then, we can begin the process of creating an abstract base class by inheriting ABC.
To create an abstract method we'll decorate enroll with abstractmethod. Since it must be implemented later, it's standard practice to only add pass to an abstract method's body.
Here, we've created an abstract base class called School, with a single abstract method, enroll. Any class that wishes to follow the School blueprint, must include an implementation of the enroll method.
Abstract base classes can also include concrete methods. These methods are inherited by any class implementing the abstract base class, rather than needing to provide their own definition of the method. The graduate concrete method here prints a short congratulatory message.
4. Implementing abstract base classes
Now, can define a new class called HighSchool, which inherits from School. In order to follow the blueprint set out by School, HighSchool must define an enroll method. Otherwise, a TypeError will be raised.
When an instance of the HighSchool class is created and the "enroll" method is called, a short message is printed.
Since School defines graduate as a concrete method, it's inherited by HighSchool, rather than needing to implement it itself. When the "graduate" method is called, the message "Congrats on graduating!" is printed.
5. Multiple abstract methods
As you might have guessed, abstract base classes can contain more than one abstract method. On the left, we've updated School to define two abstract methods; enroll and add_course.
To match the blueprint of the School abstract base class, HighSchool has been updated to include an add_course method, which takes a single parameter course_name, and appends it to the self-dot-course attribute.
6. Let's practice!
Awesome work! It's time to practice creating and implementing abstract base classes with a few hands-on exercises. Good luck!