Inheritance in Python
Inheritance in Python
Contents
What is Inheritance?
When to Use Inheritance?
Types of Inheritance
Single Inheritance
Init Method
Method Overriding
The super() method
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Other Special In-Built Functions to Check Inheritances
Conclusion
What is Inheritance in Python?
Inheritance in Python is referred to as the ability where the child class (derived class) inherits
or acquires all the properties and behaviors from the parent class (base class).
In object-oriented programming, inheritance is largely used to improve code reuse, by that I mean
the new class can automatically use all the attributes and methods of the old class without having
to copy the code again. As a result, you can declare what is unique to your new class, which will
override the old class’s behaviour. The old class is called superclass, parent, or base class and
the class that inherits from a parent (or super) class is called a subclass, child, or derived class.
Let’s decipher this with the help of an example. You might have heard of the popular Spanish
thriller series La Casa de Papel (aka Money Heist). Well, the series showcases a professor and his
group of robbers as they indulge in nerve-wracking heists and take hostages along the way with
their signature red jumpsuits and Dalí masks. It’s hard to imagine a thriller without its main
characters. These main characters can be categorized like you have a professor, a gang of robbers,
then there are law enforcement officers, politicians, etc. Now, creating a separate class for each of
these individuals is pointless.
Instead, you can have a superclass called MoneyHeist_Charecters and use this as a parent class
for the Robber class, Law Enforcement Officers class. The parent class can have common
properties and behaviors shared by all the child classes. In addition, each child class can have its
own properties and classes.
Well, I guess that’s enough theory! Let’s try to grasp the concept by actually implementing it.
Let’s suppose that we’re creating an inventory application for a pet sanctuary. Here, we can name
different kinds of objects such as parrots, cats, dogs, hamsters, rabbits, etc. Now, does it make to
write code for each cat, each dog, and so on?
There is a subclass called Cat that inherits the attributes and methods of the base class
Pet.
In addition, the class Cat also has its own method, sounds
We have used special Python object initialization method __init__()
Okay, I see that there are a few new terms like __init__, self, method overriding, etc. Let’s
explore them in detail.
__init__ method
The line pet1 = Pet(‘cat’, ‘Tiffany’, ‘2019-07-08’) from the previous example creates a new
instance of Pet class, then initializes its data by calling the class’s __init__ method. The __init__
function runs automatically when the class is called. Every class that you create can provide an
__init__ method that specifies how to initialize an object’s attributes.
#constructor
def __init__(self, pet_type, name, bdate):
self.pet_type = pet_type
self.name = name
self.bdate = bdate
You can actually write your own version of __init__() method, i.e. override it.
‘self’ in Python
Most often the very first argument of a class method is self. The argument self just points to the
instance of our class that we’re currently working with. From Python’s perspective it’s just a
convention, I mean the name self has absolutely no special meaning. It simply binds the
arguments passed with the object’s attributes.
Method Overriding
So far, we have looked at the class Cat which inherited all the attributes and behaviors of parent
class Pet. In addition, the class Cat also created its own unique method, that is specific to the
derived class. However, the child class can create its own version of the inherited attributes and
methods by a technique called overriding. We call this method overriding.
The Dog class overrides a parent method Details and creates its own version. The line pet2 =
Dog(‘Dog’, ‘Toby’, ‘2018-07-08’, ‘bull dog’), invokes the class Dog version of details().
You can override any method of the parent class, including __init__(). In the above example,
notice that __init__ method is defined in both classes. When this happens, the method in the child
class overrides the method in the parent class. Basically, by overriding, we are trying to extend
the definition of the class rather than simply replacing it.
In simple terms, you can still use the parent class version of the method and append to it the
additional details from the child class. This can be achieved using built-in-function super(). By
using the function super(), we are basically bringing a parent method into the child method to
make use of it. The super() in the below example gets the definition of Pet, the parent class.
Now that you are clear on these important topics related to inheritance, let’s go back to discussing
different types of inheritance.
The concept details that we can learn from this example are:
There are have 2 subclasses Cat and Dog that inherit the attributes and methods of the
base class Pet. In addition they also have their own attributes and methods
A child class is simply a specialization of parent class
Both Cat and Dog override a parent method Details and create their own version. You can
override any method, including __init__(). This is called method overriding
The class Dog has a method called sounds, which is not present in its parent class Pet
Now you know how to add or override a parent class method. You have two versions of
same method. How to call the parent version of that method? In such scenario fucntion
super() comes to your aid
The super() brings in the definition of parent class Pet into child class
The __init__() method automatically takes care of passing the argument self
to parent class. All you need is to give optional arguments if there are any
In the class Dog, the line self.breed = breed and the instance method sounds is the new
code that makes class Dog different from a Pet
This function is used to check if a class is derived from a particular base class. If the classes share
a parent-child relationship, it returns a Boolean value of True, else it returns False.
You can also explore- How to Find the Factorial of a Number Using Python
isinstance( )
This function is used to check if an object is an instance of a particular class or any of the classes
it has been derived from. This function takes two arguments – the object, and the class we are
checking it against. If the object is an instance, it returns a Boolean value of True, else it
returns False.
class parent:
def display(self):
print("Parent Class Method")
class child(parent):
def display(self):
super().display()
print("Child Class Method")
#Check if child is derived from parent
print(issubclass(child,parent))
#Check if parent is derived from child
print(issubclass(parent,child))
X = child()
Y = parent()
#Check if object X is instance of child
print(isinstance(X,child))
#Check if object X is instance of parent
print(isinstance(X,parent))
#Check if object Y is instance of child
print(isinstance(Y,child))
#Check if object Y is instance of parent
print(isinstance(Y,parent))
Output:
Conclusion
That’s it guys! Hope you found this interesting. To summarize, inheritance supports code
reusability, by allowing you to create a generic parent class and a more unique child class, which
automatically gets access to functionalities of the parent class once it inherits the parent class.