
Classes and objects make up the core functionality of the python Programming language. Classes provide a convenient way to organize attributes (data) and methods (functions that act on data). An important concept in object oriented programming is class inheritance. Inheritance allows us to define a class that takes all functionality from a parent class while adding additional data and/or methods. In this post, we will define a parent class and child class and demonstrate the use of inheritance.
Let’s get started!
To begin, let’s define a class called ‘Spotify_User’. The class will contain an ‘init()’ method which will allow us to initialize user object values like name, email, and premium user status:
class Spotify_User:
def __init__(self, name, email, premium):
self.name = name
self.email = email
self.premium = premium
Next, let’s define a method that allows us to check whether or not a user is a premium user:
def isPremium(self):
if self.premium:
print("{} is a Premium User".format(self.name))
else:
print("{} is not a Premium User".format(self.name))
Now, let’s define two ‘Spotify_User’ objects:
user_1 = Spotify_User('Sarah Phillips', '[email protected]', True)
user_2 = Spotify_User('Todd Grant', '[email protected]', False)
We can check whether the user is a ‘premium user’ using the ‘isPremium’ method:
user_1.isPremium()
user_2.isPremium()

Now that we have our parent class defined, let’s define a child class called ‘Premium_User’. The class name follows the structure ‘ChildClass(ParentClass)’, where the parent class is an argument of the child class. Let’s assume Spotify has different subscription tiers. The child class constructor will initialize the attribute ‘subscription_tier’:
class Premium_User(Spotify_User):
def __init__(self, subscription_tier):
self.subscription_tier = subscription_tier
Next, we will use the ‘super()’ method which will allow the child class to call the parent class constructor:
class Premium_User(Spotify_User):
def __init__(self, subscription_tier, name, email, premium):
self.subscription_tier = subscription_tier
super(Premium_User, self).__init__(name, email, premium)
Now, let’s add an additional class method to our ‘Premium_User’ class. This method will check the ‘subscription_tier’ value of the instance and print the content available to that tier:
class Premium_User(Spotify_User):
...
def premium_content():
if self.subscription_tier == 'Tier 1':
print("Music streaming with no advertisements")
if self.subscription_tier == 'Tier 2':
print("Tier 1 content + Live Concert Streaming")
Let’s now define a new user, with a ‘Tier 1’ subscription:
user3 = Premium_User('Tier 1', 'Megan Harris', '[email protected]', True)
Let’s use the parent class method, ‘isPremium’, to check if our new user is a premium user:
user3.isPremium()

Next, we can use the method from the child class, ‘premium_content’, to see what content is available to ‘Tier 1’ subscribers:
user3.premium_content()

We can also define a ‘Tier 2’ subscriber:
user4 = Premium_User('Tier 2', 'Bill Rogers', '[email protected]', True)
And check the content available:
user4.premium_content()

I’ll stop here but feel free to play around with the code.
CONCLUSIONS
To summarize, in this post we demonstrated the use of inheritance with parent and child classes in Python. In our examples, we were able to see how, through inheritance, a child class can take all of the data and methods of a parent class while adding additional data and methods to the child class. I hope you found this post useful/interesting. The code from this post is available on GitHub. Thank you for reading!