Inheritance in Python
One of the core concept in OOPs language is Inheritance.It is a mechanism that allows you to create a hierarchy of classes that share a set of properties and methods by deriving a class from another class. Inheritance is the capability of one class to derive or inherit the properties from another class.
Python Inheritance Syntax :
The syntax of simple inheritance in Python is as follows:
Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}
Parent Class:
A Parent class is a class whose properties are inherited by the child class. Let’s create the parent class that is person. the Person class having the two methods and it having three parameter default is self, fname and lname. when we create a object i.e x and using object called the method pritname then we get the following output.
Child class :
A child class is a class that drives the properties from its parent class. Here Emp is another class that is going to inherit the properties of the Person class(base class).
Here, Person is parent class. It having the two method __init__ and printname. EMp is nothing but the child class which is inherited from Person class. when we create a object ‘x’ for Emp class then automatically called the Person’s class method because person class is parent class and parent class properties are inherited by child class, so we get following output.
Types Of Inheritance :
Types of Inheritance depend upon the number of child and parent classes involved. There are four types of inheritance in Python:
- Single inheritance
- Multiple inheritance
- Hybrid inheritance
- Hierarchical inheritance
- Multilevel inheritance
1. Single Inheritance :
single inheritance is considerd as one child class that is inherite from only one parent class.
Syntax :
class Base_class:
<Base class statements>
class DerivedClass(Base_class):
<Derived class statements>
Example :
In above example class A is Parent class and class B is child class which is derived from parent class A.
2. Mutilevel Inheritance :
When one class inherits from another class, each derived class will act as an intermediate base class to the next derived class. There is no limit for extending derived classes.
Get Vrushali Walekar’s stories in your inbox
Join Medium for free to get updates from this writer.
Class A is parent class. The class B is child class of A and parent class for C and class C is a child class for B and grandchild class of A.
Syntax :
class Base_class:
<base class statements>
class derived_class1(Base_class):
<Derived_class1, Base_class statements >
class derived_class2(derived_class1):
<Base_class, derived_class1, derived_class2 statements>
Example :
3. Multiple Inheritance :
Python can support multiple inheritances, when a new derived class inherits features from more than one base classes. For example, a child can get attributes from his father and mother.
Syntax :
class base_A:
<logic>
class base_B:
<logic>
class derivedclass_C(base_A,base_B):
<logic>
Example :
4 . Hierarchical Inheritance :
A hierarchical inheritance works well in python like other object-oriented languages. When more than one derived class extended from one base class consider as a hierarchical inheritance.
Syntax
class class_A:
<Base class statements>
class class_B(class_A):
<class_B and class_A statements>
class class_C(class_A):
<class_C and class_A statements>
Example :
5. Hybrid Inheritance:
Python provides us the hybrid inheritance for handling more than one type of inheritance in a program. A combination of more than one inheritance, multilevel and hierarchical inheritance work together (inherit its properties) in a program called as Hybrid inheritance.
Let’s see a syntax for hybrid inheritance.
Syntax :
class class_A:
<logic>
class class_B(class_A):
<logic>
class class_C(class_A):
<logic>
class class_D(class_B,C):
<logic>
Example :
I hope, you understood very well about the Inheritance and its types. If you are getting any queries while doing above mentioned, feel free to comment below, and stay tuned, we will see the rest of OOPs concepts in the next article.
Thanks for reading !!








