Python ProgrammingPython Programming

Python Inheritance

In this tutorial you will learn, how to achieve single and multiple inheritance in Python.

Just like Java or C++, Python also supports the concept of both multiple and multilevel inheritance.

Inheritance is the ability to define a new class that is a modified version of an existing class. The new class inherits the members of the class it extends.

When a new class inherits from an existing one, the existing one is called the parent class(super-class) and the new class is called the child class(sub-class).


Simple Inheritance

Inheritance can be achieve by passing the parent class as an argument in the class definition of child class.

Example
The ch1 object is an instance of the Child class, but it has an access to data attributes and methods described by both Parent and Child classes.
# Parent class created
class Parent:
    parentname = ""
    childname = ""
 
    def show_parent(self):
        print(self.parentname)
 
 
# Child class created inherits Parent class
class Child(Parent):
    def show_child(self):
        print(self.childname)
 
 
ch1 = Child()  # Object of Child class
ch1.parentname = "Mark"   # Access Parent class attributes
ch1.childname = "John"
ch1.show_parent()   # Access Parent class method
ch1.show_child()    # Access Child class method

Multiple Child Classes

In this type of Inheritance two to more child classes can inherit from a parent class.

Example
Two child classes Son and Daughter inherits the same parent class named Parent.
# Parent class created
class Parent:
    parentname = ""
    childname = ""
 
    def show_parent(self):
        print(self.parentname)
 
 
# Son class inherits Parent class
class Son(Parent):
    def show_child(self):
        print(self.childname)
 
 
# Daughter class inherits Parent class
class Daughter(Parent):
    def show_child(self):
        print(self.childname)
 
 
s1 = Son()  # Object of Son class
s1.parentname = "Mark"
s1.childname = "John"
s1.show_parent()
s1.show_child()
 
d1 = Son()  # Object of Daughter class
d1.childname = "Riya"
d1.parentname = "Samule"
d1.show_parent()
d1.show_child()

Multiple Parent Classes

In multiple inheritance one child class can inherit multiple parent classes.

Example
Two parent classes Father and Mother extended by the child class named Son.
# Father class created
class Father:
    fathername = ""
 
    def show_father(self):
        print(self.fathername)
 
 
# Mother class created
class Mother:
    mothername = ""
 
    def show_mother(self):
        print(self.mothername)
 
 
# Son class inherits Father and Mother classes
class Son(Father, Mother):
    def show_parent(self):
        print("Father :", self.fathername)
        print("Mother :", self.mothername)
 
 
s1 = Son()  # Object of Son class
s1.fathername = "Mark"
s1.mothername = "Sonia"
s1.show_parent()

Multilevel Inheritance

In this type of inheritance, a class can inherit from a child class or derived class.

Example
Son class inherited from Father and Mother classes which derived from Family class.
class Family:
    def show_family(self):
        print("This is our family:")
 
 
# Father class inherited from Family
class Father(Family):
    fathername = ""
 
    def show_father(self):
        print(self.fathername)
 
 
# Mother class inherited from Family
class Mother(Family):
    mothername = ""
 
    def show_mother(self):
        print(self.mothername)
 
 
# Son class inherited from Father and Mother classes
class Son(Father, Mother):
    def show_parent(self):
        print("Father :", self.fathername)
        print("Mother :", self.mothername)
 
 
s1 = Son()  # Object of Son class
s1.fathername = "Mark"
s1.mothername = "Sonia"
s1.show_family()
s1.show_parent()
 

Call Parent Class Constructor from Child Class

In this type of inheritance parent class attributes initialized using child class constructor.

Example
Inside the Father class constructor, the Family class constructor is called and arguments for the Family class constructor are passed to it.
class Family:
    # Parent class constructor
    def __init__(self, name):
        self.name = name
 
 
# Father class inherited from Family
class Father(Family):
    # Child class constructor
    def __init__(self, name, age):
        #  Parent class constructor called from child class
        Family.__init__(self, name)
        self.age = age
 
 
f = Father("Mark", 36)
print(f.name)
print(f.age)