Understanding Inheritance in Python: Building a Model with Classes
In this article, we are going to talk about inheritance in Python. We will see when to use it and how to implement it. Inheritance is one of the pillars of object-oriented programming.
For this article, our problem is to build a model for a customer relationship management (CRM) application. We will take a tiny part of the CRM model to understand inheritance.
We want to manage the information of our customers and suppliers and for that, we will define two classes Customer
and Supplier
to hold the properties and methods of both of them.
This article is part of the Learn Python From Scratch series.
The Source code of this article can be downloaded from my GitHub Python repository.
What Is Inheritance?
A class in python can have properties and methods as we have seen in the article about OOP before.
A class (called child or subclass) may share the same properties and methods of another class (called base class or superclass) can inherit them from other classes without the need to implement them again.
We say that the subclass inherits from or extends the superclass.
Whenever we have a relation of type Is-A between classes, we can use inheritance among them. For example, in our problem, the customer is a business partner so we can implement inheritance and the same goes for the supplier.
The subclass can also have its own properties and method, in addition to the inherited ones. For example, our customer will have a credit limit property while the supplier will have an email.
Building The Model For Our Problem
A Customer object would have a name
and credit_limit
properties. While a Supplier will have a name
and an email
.
As we noticed both of our entities have a common property, the name
, so reimplementing it would be an unnecessary thing, right?
In this case, it’s only one property but in reality, there would be many properties.
What we want to do is to define a supper type that has all the common properties or methods, which is in our case the property name
. We’ll call it the BusinessPartner
class.
Now the Customer
can inherit or extend BusinessPartner
and implement its own additional properties or methods only. And the same goes for the Supplier
.
Implementing Inheritance
First, let’s implement our superclass BusinessPartner
which for simplicity will only have a name as a property.
We are going to add our classes in a file called models.py and in the main file we’ll import it:
class BusinessPartner:
def __init__(self, name):
self.name = name
Then in the Customer
class, we will inherit from the BusinessPartner.
We simply add the name of the superclass BusinessPartner
between two brackets after the name of the Customer class:
class Customer(BusinessPartner):
This means that the Customer
inherits or extends the BusinessPartner
class. So there is no need to define the properties or methods that are already defined in the superclass, which in our case is the name
property, and we can directly access it from within a customer object.
So in our __init__
method, we will add a name parameter but use a method called super()
to call the superclass __init__
method to set the value of the superclass’s variable name
:
super().__init__(name)
The super() method allows us to access the methods of a superclass from within a subclass.
Then we proceed with setting the credit_limit
variable value:
class Customer(BusinessPartner):
def __init__(self, name, credit_limit):
super().__init__(name)
self.credit_limit = credit_limit
The same goes for our subclass Supplier
.
Multiple inheritances
Not all programming languages allow multiple inheritances but Python allows it. This means that a class can inherit from one or many classes directly and at the same time.
Consider we have another class called Person
, the Customer class can also inherit from it as well since a customer is a person. We write the name of classes as a comma-separated list:
class Customer(BusinessPartner, Person):
However, we are not going to do that in our example.
Multilevel Inheritance
A class may inherit from another subclass as well and to as many levels as we want. For example, the BusinessPartner
class can inherit from a class Person
or we can have another subclass inherits from the Customer
class.
Actually, all classes in Python inherit from a single class by default called object
and it is the base class of all classes.
Method Overriding
Methods are inherited from the superclass and can be used from within the child objects, however, they can be overridden in the subclass to implement more specific functionality.
Let’s say we have a method in our BusinessPartner
class called get_info()
which displays the information of the object:
def get_info(self):
print("name:", self.name)
As you see, if we need to display the information of a customer we’ll need more than just the name field, so what we do is override this method in the Customer
class to add the rest of the fields:
def get_info(self):
print("name:", self.name, "credit limit:", self.credit_limit)
If we call the overridden method, which version will be executed?
Python checks the type of the object used in executing the method, if we use for example a Customer
object then the method that is implemented in the Customer
class will be called if it was overridden.
If it was not overridden, like in the case of the Supplier
class, then the method in the directly inherited upper class, the BusinessPartner
in our case, will be executed.
In the case of multilevel inheritance, we continue going up in the chain of inheritance until we hit an implemented method.
Testing The Overridden Methods
We’ll create two objects of type Customer
and Supplier
and call the method get_info()
:
customer1 = Customer("John", 1000)
suplier1 = Supplier("Tesla", "info@tesla.com")
customer1.get_info()
suplier1.get_info()
Since we override the method in the Customer
class, it was executed. However, in the Supplier
class we didn’t, so the method of the parent was executed:
name: John , credit limit: 1000
name: Tesla
The Model Code
This is our model full code with the three classes:
Conclusion
In this article, we have learned about inheritance in Python. We saw how to implement it and discussed multilevel, multiple, and method override. We also build the model for our problem using inheritance.
This article is part of the Learn Python From Scratch series.