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.
# 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.
# 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.
# 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.
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.
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)