1. Interfaces
Alright, we've explored abstract base classes, but now, it's time to take a look at their cousin, interfaces.
2. Interfaces
Interfaces are special classes made up of only abstract methods, creating a contract with the classes that would like to adhere to, or implement the interface.
This contract requires all abstract methods in the interface to be defined by any class that would like to implement the interface. If a class doesn't define an abstract method present in the interface, the contract is not fulfilled.
Abstract methods provide a skeleton of a method definition, only including the method name, parameters, and pass keyword. Parameters in an abstract method typically act as a guideline for all classes implementing the interface, but don't need to match exactly.
Interfaces can be either formal or informal. Let's start with informal interfaces.
3. Informal interfaces with duck typing
Informal interfaces are created using duck typing. Duck typing is a commonly-used phrase in programming that goes something like this; if it looks like a duck, and acts like a duck, it's probably a duck.
Here, we've created a Course class, with two methods that each pass when executed. In this case, Course is the duck; an informal interface with two abstract methods. Now, let's use duck typing to implement it.
4. Informal interfaces with duck typing
The ProgrammingCourse class here defines the assign_homework and grade_assignment methods; it looks, and feels like the Course interface.
Using more technical jargon, the ProgrammingCourse class informally fulfills the contract set out by the Course interface, without even inheriting the Course class. However, this contract is not actually enforced in any way. To do this, we'll want to use formal interfaces.
5. Formal interfaces with ABC, @abstractmethod
Formal interfaces are defined like abstract base classes, using the ABC class and abstract method decorator. Here, we've recreated the Course interface formally, this time inheriting ABC, and defining assign_homework and grade_assignment using the abstractmethod decorator.
But if interfaces are defined just like abstract base classes, then what's the difference?
6. Interfaces vs. Abstract Base Classes
Well, interfaces define a contract with the classes that implement it, creating a "must-do" relationship. Abstract base classes create a blueprint for classes with common characteristics via an "is-a" relationship.
Abstract base classes are almost always formally defined, while interfaces can be either formal or informal.
Most notably, interfaces can only define abstract methods, whereas abstract base classes can implement a combination of both abstract base classes and concrete methods.
Interfaces are typically used to create nearly identical classes that can be easily interchanged, whereas abstract base classes help to create classes that look and feel similar.
7. Implementing a formal interface
Like with abstract base classes, the class implementing a formal interface will inherit the interface itself. As we'll see in a second, this ensures the interface is properly implemented.
Here, the ProgrammingCourse class again defines the assign_homework and grade_assignment methods to fulfill the contract set out by Course. A constructor is added to the class definition, allowing an object to be created as shown here.
8. Failing to implement a formal interface
If ProgrammingCourse fails to define the grade_assignment method, we'll encounter an error when creating a ProgrammingCourse object, preventing the class from being instantiated without grade_assignment.
Using the abc module to create a formal interface ensures the interface is actually fulfilled by the class implementing it.
9. Let's practice!
Great, it's time to practice building and implementing interfaces!